Hi everyone,
I need to put values into a matrix initialized with 2 rows and 0 columns. I have tried the following three ways but without success. Please inform me the correct way.
First
y=zeros(Float16,2,0)
display(y)
for i1 in 1:1:10
push!(y[1,:],i1)
end
for i2 in 1:1:10
push!(y[2,:],i2)
end
display(y)
Second
y=zeros(Float16,2,0)
display(y)
for i1 in 1:1:10
push!(y[1,],i1)
end
for i2 in 1:1:10
push!(y[2,],i2)
end
display(y)
Third
y=zeros(Float16,2,0)
display(y)
for i1 in 1:1:10
push!(y[1],i1)
end
for i2 in 1:1:10
push!(y[2],i2)
end
display(y)
The problem is that the memory is “linear”, thus it is not possible to simply add a row to a matrix without allocating the matrix again, or making the memory layout discontinuous. Maybe consider the use of an array of arrays.
No, you could certainly do it, simply by allocating padding in the middle of the array (in a geometrically increasing fashion). It would just act like a view of the underlying data, and you’d even be able to use LAPACK and BLAS (since they support such padding).
(Technically, you could also do an in-place matrix transpose to row-major format, add the row by growing the end of the array, and then transpose back, but that’s pretty inefficient.)
You realize that Matlab is just copying the data to a new arrays when you grow it, right? You can do the same thing in Julia.
(Matlab’s matrix data structure assumes contiguous data, just like a Julia Matrix, so you can’t efficiently add rows without copying to a new array.
But the layout of the matrix would be discontinuous, or I am missing something? Even if you can do matrix operations with that, probably they become much less efficient, don’t they?
Edit: just read the GitHub there and it is clear now. Thanks.
I actually did not know how Matlab stored data. I learnt the language superficially. Thanks for the link. So (as my first post shows) I did try using push!() on a matrix initialized with 2 rows and a column, but no success.
Even if you can do matrix operations with that, probably they become much less efficient, don’t they?
No. Each column is still contiguous in memory, there is just a gap between the columns. You access A[i,j] in exactly the same way — at a memory offset lda*j + i … the only difference from a contiguous array is the value of the leading-dimension (column stride) lda.