Of course, that's the best way to do it -- Thanks. -- mb
On Tue, Oct 14, 2014 at 7:34 AM, Johan Sigfrids <[email protected]> wrote: > You can tell ones to construct a array on Ints by passing it a type > parameter: > > ones(Int, 1, 3) > > With a length of three it doesn't make much difference but with a bigger > array you save a lot of time avoiding the conversion. > > On Tuesday, October 14, 2014 3:14:38 AM UTC+3, Miguel Bazdresch wrote: >> >> Just wanted to point out a little-known way to achieve the same with the >> `kron` (Kronecker product) command: >> >> julia> x=[1 2 3 4] >> 1x4 Array{Int64,2}: >> 1 2 3 4 >> >> julia> kron(x,int(ones(1,3))) >> 1x12 Array{Int64,2}: >> 1 1 1 2 2 2 3 3 3 4 4 4 >> >> -- mb >> >> On Mon, Oct 13, 2014 at 6:05 PM, Rajn <[email protected]> wrote: >> >>> >>> >>> Hi, >>> I have two questions. >>> >>> First question: >>> >>> I have a fairly simple problem which can be solved in many ways. >>> >>> To begin I have a vector >>> z1=Array(Float64,1,0) >>> >>> I have >>> Z=1:0.01:100 >>> >>> I would like to generate a vector in which each element is repeated >>> length(Z) times i.e., [1 1 1 1....length(Z) times 1.01 1.01 >>> 1.01....length(Z) times and so on >>> >>> When I do >>> for j=1:length(Z) >>> z1=[z1 repmat([Z[j]],1,length(Z)] >>> end >>> >>> and compare the time for this exact code with Matlab's/Octave, Julia >>> takes 7 seconds in comparison to 1.5 seconds for Matlab/Octave. >>> >>> Of course I did not see any improvement using >>> z1=[z1 ones(1,length(Z))*Z[j]] >>> >>> It took practically the same time. >>> >>> However, this reduced to fraction of a second...i.e., after I generated >>> a vector of zeros and just replaced the zeros appropriately. >>> z1=zeros(1,length(Z)^2) >>> length(Z)=dd >>> for j=1:dd >>> v=1:dd:(dd-1)*dd >>> z1[1,v[j]:(v[j+dd-1])]=Z[j]; >>> end >>> >>> Of course even Octave/Matlab became equally fast with this code. >>> >>> So what gives in repmat for Julia? Why is it slow? Have I gone wrong >>> with my first code? >>> >>> >>> >>> >>> Question 2: >>> If I start with A=[] >>> How do I vcat or hcat a row or a column vector to A in a for loop form a >>> MATRIX. >>> say my vector is A(10x1) and I want to make B=[A[1] A[2] A[3] A[4]] >>> which is a 10x4 matrix. >>> In Octave I would do >>> B=[] >>> B=[B A[j]] in a for loop. >>> >>> Thanks >>> >> >>
