As long as each row has a fixed, known size, you could do

a = []
for i=1:n
    append!(a, [1,2,3])
end
A = reshape(a, 3, n)

The 1-D array grows as needed, and reshape still points to the original
data, so no copying is done.

Cheers,
   Kevin

On Mon, Jan 18, 2016 at 12:48 PM, Júlio Hoffimann <[email protected]
> wrote:

> Yes, I will rely on the classical hcat() approach...
>
> A = []
> for i=1:n
>   push!(A, [1,2,3])
> end
> A = hcat(A...)
>
> Thank you.
>
> 2016-01-18 11:42 GMT-08:00 Júlio Hoffimann <[email protected]>:
>
>> Hi,
>>
>> Suppose I want to fill the columns of a matrix which size I don't know
>> beforehand:
>>
>> A = zeros(3,0)
>> for i=1:n
>>   A = [A [1,2,3]]
>> end
>>
>> Is there a memory efficient way of doing that in Julia?
>>
>> I understand that the above syntax is allocating 3*i entries at iteration
>> i which gives 3*(1+2+...+n) = 3*(n+1)n/2 allocations as opposed to 3n.
>>
>> -Júlio
>>
>
>

Reply via email to