Vector of Vectors to Create Matrix

Vector of Vectors to create matrix

As it is, both dimensions of your vector are 0.

Instead, initialize the vector as this:

vector<vector<int> > matrix(RR);
for ( int i = 0 ; i < RR ; i++ )
matrix[i].resize(CC);

This will give you a matrix of dimensions RR * CC with all elements set to 0.

How do I make a matrix from a list of vectors in R?

One option is to use do.call():

 > do.call(rbind, a)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 2 3 4 5
[2,] 2 1 2 3 4 5
[3,] 3 1 2 3 4 5
[4,] 4 1 2 3 4 5
[5,] 5 1 2 3 4 5
[6,] 6 1 2 3 4 5
[7,] 7 1 2 3 4 5
[8,] 8 1 2 3 4 5
[9,] 9 1 2 3 4 5
[10,] 10 1 2 3 4 5

using broadcasting Julia for converting vector of vectors to matrices

If your vectors are short and of fixed size (e.g., a list of points in 3 dimensions), then you should strongly consider using the StaticArrays package and then calling reinterpret. Demo:

julia> using StaticArrays

julia> A = rand(3, 8)
3×8 Array{Float64,2}:
0.153872 0.361708 0.39703 0.405625 0.0881371 0.390133 0.185328 0.585539
0.467841 0.846298 0.884588 0.798848 0.14218 0.156283 0.232487 0.22629
0.390566 0.897737 0.569882 0.491681 0.499163 0.377012 0.140902 0.513979

julia> reinterpret(SVector{3,Float64}, A)
1×8 reinterpret(SArray{Tuple{3},Float64,1,3}, ::Array{Float64,2}):
[0.153872, 0.467841, 0.390566] [0.361708, 0.846298, 0.897737] [0.39703, 0.884588, 0.569882] … [0.390133, 0.156283, 0.377012] [0.185328, 0.232487, 0.140902] [0.585539, 0.22629, 0.513979]

julia> B = vec(copy(ans))
8-element Array{SArray{Tuple{3},Float64,1,3},1}:
[0.1538721224514592, 0.467840786943454, 0.39056612358281706]
[0.3617079493961777, 0.8462982350893753, 0.8977366743282564]
[0.3970299970547111, 0.884587972864584, 0.5698823030478959]
[0.40562472747685074, 0.7988484677138279, 0.49168126614394647]
[0.08813706434793178, 0.14218012559727544, 0.499163319341982]
[0.3901332827772166, 0.15628284837250006, 0.3770117394226711]
[0.18532803309577517, 0.23248748941275688, 0.14090166962667428]
[0.5855387782654986, 0.22628968661452897, 0.5139790762185006]

julia> reshape(reinterpret(Float64, B), (3, 8))
3×8 reshape(reinterpret(Float64, ::Array{SArray{Tuple{3},Float64,1,3},1}), 3, 8) with eltype Float64:
0.153872 0.361708 0.39703 0.405625 0.0881371 0.390133 0.185328 0.585539
0.467841 0.846298 0.884588 0.798848 0.14218 0.156283 0.232487 0.22629
0.390566 0.897737 0.569882 0.491681 0.499163 0.377012 0.140902 0.513979

Initialize Vector Of Vectors in C++

std::vector<std::vector<T>> my_vec(m, std::vector<T>(n))

Be careful that Ts default constructor is called for each of m * n members of the matrix.

How do you make a matrix out of vectors in eigen?

You can also append them using the comma initializer syntax:

m << v1, v2, v3, v4;

The matrix m must have been properly resized first.



Related Topics



Leave a reply



Submit