Last active
February 2, 2023 17:54
-
-
Save paulnakroshis/ca3ce8de9520c389e3144b69ba30b07b to your computer and use it in GitHub Desktop.
Vector of Vectors to Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
# Convert a vector of vectors to an array | |
This function assumes that each vector is the same length; | |
i.e. | |
v1 = [ [1,2], [2,3], [5,6] ] # this is fine | |
v2 = [ [1,2,8], [2,3], [5,6] ] # this is not fine | |
Example of usage: | |
``` | |
vv = [[1,2,3], [4,5,6], [7,8,9],[12,13,14]] | |
println("num components in each vector = ", length(vv[1])) | |
println("num of vectors = ", length(vv)) | |
vecvec_to_array(vv) | |
``` | |
""" | |
function vecvec_to_array(vecvec) | |
dim1 = length(vecvec) | |
dim2 = length(vecvec[1]) | |
my_array = zeros(Int64, dim1, dim2) | |
for i in 1:dim1 | |
for j in 1:dim2 | |
my_array[i,j] = vecvec[i][j] | |
end | |
end | |
return my_array | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment