Finding an average value using integer arrays - arrays

In my program I have designed it so that it reads from a file, parses the file for any integers and saves them to a sorted list. From that list I have then created three arrays which would each hold different values based off the values in the list except the values would go up by 1 for each array so:
Example Values:
array 1 will hold the regular values e.g. - 2,3,4,5
array 2 will hold the regular values (+1) - 3,4,5,6
array 3 will hold the regular values (+2) - 4,5,6,7
2,3,4 = 1st user's score
3,4,5 = 2nd user's score
4,5,6 = 3rd user's score
From this I now want to add these arrays together and divide by 3 to get the average results and then save and join the values with their corresponding user, however am not sure if I can add the arrays directly.
This is the code I have so far:
Dim basenum As New System.Collections.Generic.List(Of Integer)
Dim num As String
num = Regex.Replace(line, "\D+", "")
For Each lin As String In num
basenum.Add(Convert.ToInt32(lin))
Next
basenum.Sort()
basenum.Reverse()
For Each element As Integer In basenum
Console.WriteLine(element)
Next
Dim number1() As Integer
Dim number2() As Integer
Dim number3() As Integer
number1 = basenum.ToArray
For Each element As Integer In number1
Console.WriteLine(element)
Next
number2 = basenum.ToArray 'insert 2nd numbers into second array
For Each element As Integer In number2
element += 1
Console.WriteLine(element)
Next
number3 = basenum.ToArray 'insert 3rd numbers into third array
For Each element As Integer In number3
element += 2
Console.WriteLine(element)
Next
The numbers go into the arrays fine however am not sure how I can add the arrays together and calculate the average.
Any help would be appreciated guys :)

Related

Aggregating part of a 2d array in a column in said array

I have a 2d array, with flexible dimensions:
arr_emissions(1 to n, 0 to m)
Where n is 22 or larger, and m is 6 or larger.
In the smallest case column m = 6 should contain the sum of columns m = 2 - 5.
I could ofcourse simply add them, but as the dimensions of the array are flexible I would like to implement a more robust method, that preferly doesn't loop over the entire array.
I was hoping to implement the native application.WorksheetFormula.Sum(). I saw an implementation in this answer, but that only works for complete rows or columns.
Example:
I have arr_emissions(0 to 111,1 to 6). It is populated in a loop from 1 to 111.
The data in the array is as follows:
(1,1) #3-4-2020# 'a date value
(1,2) 1,379777
(1,3) 0
(1,4) Empty
(1,5) Empty
Don't know if this helps, but this takes a source array v and then populates a new array w with the sum of columns 2-4 of the corresponding row of v.
Sub x()
Dim v, i As Long, w()
'just to populate source array
v = Range("A1").CurrentRegion.Value
ReDim w(1 To UBound(v, 1))
For i = 1 To UBound(w)
'each element of w is sum of columns 2-4 of corresponding row of v
w(i) = Application.Sum(Application.Index(v, i, Array(2, 3, 4)))
Next i
'write w to sheet
Range("G1").Resize(UBound(w)) = Application.Transpose(w)
End Sub
Thanks to the answer from SJR I found myself a working solution. This is all within a larger piece of code, but for this example I filled some variables with fixed numbers to match my example from my question.
Dim days as Integer
days = 111
Dim emissions_rows as Integer
emissions_cols = 6
ReDim arr_emissions(0 To days, 1 To emissions_cols) As Variant
Dim arr_sum As Variant
Dim sum_str As String
sum_str = "Transpose(row(2:" & emissions_rows - 1 & "))"
arr_sum = Application.Evaluate(sum_str) '= Array(2,3,4,5)
arr_emissions(emissions_index, emissions_cols) = Application.Sum(Application.Index(arr_emissions, emissions_index + 1, arr_sum))
The code writes a string to include the variables, so to take the second column untill the second to last column, which is then evaluated into an array.
That array is then used within the sum function, to only sum over those columns.
The result is then written to the last column of arr_emissions().
emissions_index is an index that is used to loop over the array.

remove an item from an integer array

I'm trying to remove an element from an integer array given its index, are there any simple ways of accomplishing just that in VB.Net?
All I can find is .RemoveAt() for removing a string in an ArrayList, but none for an integer in an integer array.
Dim PossibleValues(8) As Integer
For x As Integer = 0 To 8
PossibleValues(x) = x + 1
Next
Added the code, don't think it entails much as I am now trying to find ways to remove an element from the array PossibleValues
The problem with removing an item from an array is that it is of fixed size. So The simplest way is to convert the array to a list, ToList(), and then do the RemoveAt and then, if you need to, convert it back to a new array with ToArray().
EDIT
Dim PossibleValues(8) As Integer
For x As Integer = 0 To 8
PossibleValues(x) = x + 1
Next
Dim tempList = PossibleValues.ToList()
tempList.RemoveAt(1)
PossibleValues = tempList.ToArray()

Trouble looping through a data file and storing integers in an array

I am having a problem with a loop. I am writing a program that loops through lottery drawings and does some different analysis' on the drawings. The problem I am having is since a loop is 0-based but there is no number 0 in lottery drawings I cant figure out how to start the loop at 1 instead of 0. Also, when I cut out an integer, if the integer is a single digit the loop doesn't see the zero before the single digit and counts all of the 0-9 in all of the integers. I am trying to grab an integer and then tick that element of the array. Here is the loop.
'Choices(59) is passed into the loop from a click event
Private Sub GetFrequency(Choices() As Integer)
Dim Size As Integer = UsersChosenHistory.Length() 'Size
Dim Number As Integer = 1
Dim Start As Integer = 0
Dim Finish As Integer = 3'Grab 3 chars, a space + 2 digit Integer
For i As Integer = 1 To Size - 1 Step 1
Number.ToString("d2")'I've also tried Number.ToString("D2") (Capitol D)
Number = UsersChosenHistory.Substring(Start, Finish) 'Grab an integer
Choices(Number) += "1" 'Store it in the corresponding array element
Start += 1
Next
End Sub
When running through the loop with the F11 key the single digits do not show the leading "0" even though the data file does include the "0", and as I mentioned above the array shows a "0" as the first digit in the frequence grid. I'm really confused with this loop, any help will be greatly appreciated. I'm just learning VB.Net and this has me stumped. Thanks in advance.
For i As Integer = 0 To Size - 1 Step 1
Number = int.Parse(UsersChosenHistory.Substring(i*3,3).Replace(" ",""))
Choices(Number) += "1"
Next
For the "0" issue, ignore it. Choices(0) will never be incremented.

Byte array percentage similarity

I am kinda new in VB.net; pretty much what I have been looking for is a way to calculate the similarity of a byte array. I have been able to determine whether they are equal or not, but I haven't figured out how to calculate how similar they are in percentage. Any idea is appreciated. Thanks! Leo.
The following function takes two Byte arrays as its arguments. If the arrays are not the same length, it throws an exception. Otherwise, it returns the fraction of the elements in one array that are equal to the element at the same position in the other array.
Function PercentAlike(array1() As Byte, array2() As Byte) As Double
If array1.Length <> array2.Length Then Throw New ArgumentException("Arrays must have the same length")
Dim same As Integer
For i As Integer = 0 To array1.Length - 1
If array1(i) = array2(i) Then same += 1
Next
Return same / array1.Length
End Function
[Added in response to the comment that the arrays may not be the same length]
If you need to calculate a percentage even if the arrays are of different lengths, you can use the following function. It returns the number of elements in one array that are equal to the element at the same position in the other array (ignoring the extra elements in the longer array) as a fraction of the number of elements in the longer array.
Function PercentAlike(array1() As Byte, array2() As Byte) As Double
Dim same As Integer
For i As Integer = 0 To Math.Min(array1.Length, array2.Length) - 1
If array1(i) = array2(i) Then same += 1
Next
Return same / Math.Max(array1.Length, array2.Length)
End Function

Unique values in Excel

I need to get a value which has the maximum number of occurrences in the unique data set from a big column based on a condition. I just need one value and not the array.
See below the example data that I am working with. I have done it in MATLAB but want to know it in Excel.
So in the above data for example, I want to get the unique values for lanes based on the value of #safea. So if #safea=102 then unique values of lanes=(2,3,1). But I want the value from these set of data which has maximum number of occurrences. In this case it is 2 because 2 has come up 5 times whereas 3 has come up once and 1 has come up as only 1 time.
Another example:
If I select #safea as 162, then the number of unique values in lanes (5 and 4) but 5 has come up 4 times and 4 has come up as only 1 time so the final answer that I want is '5'.
If you don't mind using VBA, I've devised a Function you can use for what you want. Given the #safea values are in column A and the lane values are in column B, you can use this:
Function MODEIF(criteria As Integer) As Integer
Dim count As Integer
count = Application.WorksheetFunction.CountA(Range("A:A"))
Dim list() As Integer
Dim size As Integer
size = 0
Do While count > 0
If (Range("A" & count) = criteria) Then
ReDim Preserve list(size)
list(size) = Range("B" & count)
size = size + 1
End If
count = count - 1
Loop
MODEIF = Application.WorksheetFunction.Mode(list)
End Function
Just put this Function in a Module, go to the spreadsheet, and type =MODEIF(102) or whatever #safea value you want the mode for and it will give you the answer.
You could also use this worksheet function to get a conditional MODE:
=MODE(IF(**your #safea value here**=$A$2:$A$22,$B$2:$B$22))
This is an array formula. Confirm entry by pressing Ctrl+Shift+Enter (not just Enter).

Resources