I have an array containing 5 integers. For example:
myArray = Array(3, 5, 2, 9, 3)
I want to write a loop that loops through the elements of the array, and on each iteration returns the product of the current element and all elements to the right of it.
In the example above:
3 * 5 * 2 * 9 * 3
5 * 2 * 9 * 3
2 * 9 * 3
9 * 3
3
My non-VBA thinking was along the lines of:
for i in myArray
product(myArray[i:])
But I don't know how to write this in VBA, especially since it doesn't support array subsetting. Any help is welcome.
You need two loops:
Sub foo()
Dim myArray
myArray = Array(3, 5, 2, 9, 3)
Dim i&, j&
Dim output As Double
For i = LBound(myArray) To UBound(myArray)
output = 1
For j = i To UBound(myArray)
output = output * myArray(j)
Next j
Debug.Print output
Next i
End Sub
Related
Lets say I have the following array:
board = np.random.randint(1, 9, size=(2, 5))
How do I remove duplicates from each element in the array
e.g.
[[6 1 2 8 4]
[8 3 2 3 6]]
So here there are two 3s, and I want one of those to be deleted, how can I perform such an action?
Given your example, it seems that you don't want repetition relatively to rows. You may be interested in numpy.random.choice and try something like this:
import numpy as np
nb_lines = 2
nb_columns = 5
min_value = 1
max_value = 9
range_value = max_value-min_value
# The number of columns should be <= than the integer range to have a solution
assert(range_value+1 >= nb_columns)
board = min_value + np.array([
np.random.choice(np.arange(range_value+1), nb_columns, replace=False)
for l in range(nb_lines)
])
print(board)
Output:
% python3 script.py
[[7 4 6 3 1]
[2 8 6 4 3]]
I have a list where each element is an array. Example data:
set.seed(24)
data <- list(individual1 = array(rnorm(3 * 3 * 2, 60),
dim = c(3, 3, 2), dimnames = list(NULL, NULL, c("rep1", "rep2"))),
individual2 = array(rnorm(3 * 3 * 2, 60), dim = c(3, 3, 2),
dimnames = list(NULL, NULL, c("rep1", "rep2")) ) )
I would like to find the length of the entire list. However, when I use length, I get 2, whereas I want 4 because there are 4 arrays. Is there another way to determine length for my question?
>length(data)
2
Like this?
sum(sapply(data, function(x) dim(x)[3]))
#[1] 4
Explanation: Your list in fact only contains 2 elements. The dimension of every list element is
lapply(data, dim)
#$individual1
#[1] 3 3 2
#
#$individual2
#[1] 3 3 2
In other words, every list elements has 2 3x3 arrays. We can therefore get the total number of 3x3 arrays in the list by summing the number of 3x3 arrays from every list element.
Suppose I have a 2-D array such that the first column is composed of only two integers 1 and 2:
1 5 1 7 0.5
2 4 5 6 0.1
1 9 3 4 0.6
2 8 7 2 0.2
I want to separate two matrices out of this, such that the first column of each contains the same integer (so the first column of first matrix contains only integer 1, same goes for 2 in the second matrix).
So it would become:
1 5 1 7 0.5
1 9 3 4 0.6
and
2 4 5 6 0.1
2 8 7 2 0.2
I don't know exactly how to start. I was thinking of using the count at the beginning (well, because I have a way larger matrix with 10 different integers in the first column), then according to the counted number of each integer I construct the dimension of each [sub]matrix. After that, the only thing I could think of is the count(mask), and if the value is true it's then added to the matrix by if statement.
You can't have mixed types (integer and real) in the same array in Fortran, so I will suppose all data are real in the 2-dim array:
program split
implicit none
real, allocatable :: a(:, :), b(:, :)
integer :: i, ids = 10
integer, allocatable :: id(:), seq(:)
a = reshape([real :: 1, 5, 1, 7, 0.5, &
& 2, 4, 5, 6, 0.1, &
& 1, 9, 3, 4, 0.6, &
& 2, 8, 7, 2, 0.2], [5, 4])
seq = [(i, i = 1, size(a, 2))]
do i = 1, ids
print*, "i = ", i
! here we are creating a vector with all the line indices that start with i
! e.g. for i = 1 we get id = [1, 3], for i = 2 we get [2, 4], for i = 3 we get [], ...
id = pack(seq, a(1,:) == i)
! here we use a Fortran feature named vector-subscript
b = a(:, id)
print*, b
end do
end
If you want the first column(or any column) to be integer, you can declare it as a separated array, and use the same vector subscripts to gather the desired lines.
So I have tried different types of visual basic code for this problem that i'm trying to solve but non of them is working :(
I have a jagged array for example {{1 10 20 50 53};{5 15 25 55}}
and I want to convert this information in a binary matrix given the condition that each array in the jagged array corresponds to a row, and the element in it corresponds to a column number. Am I being clear?
The matrix has the same rows as the number of arrays in the jagged array 2, but the columns in the matrix can go for example till 200.
1 0 ... 1 ... 1 ... 1 .. 1..................0
0 0 ... 1 ... 1 ... 1 .... 1 ...............0
My last attempt was this one:
For a = 0 To noRows - 1
For b = 0 To noCols - 1
Do While d < jarray(a).GetUpperBound(0)
If array(a)(d) = b Then
matrix(a, b) = 1
Exit Do
Else
matrix(a, b) = 0
d += 1
End If
Loop
Next
Next
What should I do?
When you make a new array its values are automatically initialised to the default value of the type of variable in the array - for an Integer, that is 0. So you don't need to do the setting to zero unless there is something else that isn't shown in the question.
You have the indexes of the locations which need to be set to 1, so you can do that directly:
Option Infer On
Option Strict On
Module Module1
Sub Main()
'Dim jArray = {New Integer() {1, 10, 20, 50, 53}, New Integer() {5, 15, 25, 55}}
Dim jArray = {New Integer() {1, 3, 4}, New Integer() {2, 5, 6, 7, 9}}
' find the number of columns needed for the resultant array
Dim nCols = jArray.Max(Function(r) r.Max())
' find the number of rows needed for the resultant array
Dim nRows = jArray.GetUpperBound(0) + 1
' the resultant array is automatically initialised to zeros
Dim matrix(nRows, nCols) As Integer
' fill in the ones as specified by the locations in jArray:
For i = 0 To nRows - 1
For j = 0 To jArray(i).GetUpperBound(0)
matrix(i, jArray(i)(j) - 1) = 1
Next
Next
' show the resultant array:
For i = 0 To nRows - 1
For j = 0 To nCols - 1
Console.Write(matrix(i, j) & If(j < nCols - 1, ", ", ""))
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module
Outputs:
1, 0, 1, 1, 0, 0, 0, 0, 0
0, 1, 0, 0, 1, 1, 1, 0, 1
I have a large array of single digit integers (0 - 9) and I would like to turn a subset of the values into a single integer value. For example:
Array(0) = 4
Array(1) = 2
Array(2) = 1
Array(3) = 6
Array(4) = 7
Array(5) = 4
Array(6) = 8
Array(7) = 2
Array(8) = 9
Array(9) = 0
I would like the then make a number using (for example) array indexes 4,5 and 6;
NewInt = 748
The only way i can see of doing this is making a string and then an integer (as in CInt(Cstr(Array(5) & Array(6)...)) but this seems needlessly slow. The whole point of what I'm doing is about speeding things up.
Any suggestions?
Take a look at this.
Dim nums() As Integer = {4, 2, 1, 6, 7, 4, 8, 2, 9, 0}
Dim ans As Long = 0L
Dim useNums() As Integer = {4, 5, 6}
For Each idx As Integer In useNums
ans = (ans * 10L) + nums(idx)
Next