Re: [julia-users] upgrade/downgrade between Array{Float64,1} and Array{Float64,2}
Thanks for the explanation!! That's exactly what I need at the moment, though I clearly know that unpredictable return type could be dangerous. Many thanks. On Sunday, May 15, 2016 at 9:07:12 PM UTC+1, Kristoffer Carlsson wrote: > > It is possible but seems like something undesirable because you then don't > know if an Array{T,1} or Array{T,2} will be returned by the function. This > is something Julia people like to call "type instability" when the type of > your returned variables depend not only on the type of the arguments to the > function. > > If you really want this, then this is an example: > > function maybevec(A::AbstractMatrix) > if size(A, 2) == 1 || size(A, 1) == 1 > return vec(A) > else > return A > end > end > > > julia> maybevec(rand(5,5)) > 5×5 Array{Float64,2}: > 0.542099 0.901245 0.267711 0.260192 0.395683 > 0.361152 0.373492 0.231736 0.259319 0.933759 > 0.307834 0.460153 0.157018 0.712722 0.194619 > 0.869251 0.947844 0.53740.715395 0.67132 > 0.928848 0.642071 0.948639 0.693701 0.562648 > > > julia> maybevec(rand(5,1)) > 5-element Array{Float64,1}: > 0.579539 > 0.012443 > 0.307975 > 0.742628 > 0.997113 > > > > On Sunday, May 15, 2016 at 9:17:27 PM UTC+2, chobb...@gmail.com wrote: >> >> Thanks for the pointer for reshape. >> >> Sorry that I failed to make my question clear enough. In fact, I wonder >> if there is a function which can _automatically_ recast a single-column >> Array{T,2} to an Array{T,1} and do nothing if this Array{T,2} has multiple >> rows. >> >> >> >> >> >> On Sunday, May 15, 2016 at 6:41:51 PM UTC+1, Tim Holy wrote: >>> >>> See reshape and vec. >>> >>> Best, >>> --Tim >>> >>> On Sunday, May 15, 2016 09:00:33 AM chobb...@gmail.com wrote: >>> > Here is rookie question, which I have tried to find a similar question >>> and >>> > answer in the history of this group, but failed. Please bear me. >>> > >>> > Since Julia distinguishes between Array{Float64,1} and >>> Array{Float64,2}, >>> > even there is only one column in an Array{Float64,2}. Therefore, the >>> type >>> > of rand(5) and rand(5,1) are not same. >>> > >>> > julia> x = rand(5) >>> > 5-element Array{Float64,1}: >>> > 0.183247 >>> > 0.480208 >>> > 0.623144 >>> > 0.342792 >>> > 0.0400695 >>> > >>> > julia> y = rand(5, 1) >>> > 5x1 Array{Float64,2}: >>> > 0.553224 >>> > 0.0061835 >>> > 0.762059 >>> > 0.643796 >>> > 0.655135 >>> > >>> > >>> > Is there any way that I can upgrade x about to become an >>> Array{Float64,2}? >>> > And is there a way to downgrade y to become an Array{Float64,1}, when >>> it >>> > has only one column? >>> > >>> > In case that there is a similar discussion before, a link to the >>> previous >>> > thread is good enough and appreciated! >>> > >>> > Thanks! >>> >>>
Re: [julia-users] upgrade/downgrade between Array{Float64,1} and Array{Float64,2}
It is possible but seems like something undesirable because you then don't know if an Array{T,1} or Array{T,2} will be returned by the function. This is something Julia people like to call "type instability" when the type of your returned variables depend not only on the type of the arguments to the function. If you really want this, then this is an example: function maybevec(A::AbstractMatrix) if size(A, 2) == 1 || size(A, 1) == 1 return vec(A) else return A end end julia> maybevec(rand(5,5)) 5×5 Array{Float64,2}: 0.542099 0.901245 0.267711 0.260192 0.395683 0.361152 0.373492 0.231736 0.259319 0.933759 0.307834 0.460153 0.157018 0.712722 0.194619 0.869251 0.947844 0.53740.715395 0.67132 0.928848 0.642071 0.948639 0.693701 0.562648 julia> maybevec(rand(5,1)) 5-element Array{Float64,1}: 0.579539 0.012443 0.307975 0.742628 0.997113 On Sunday, May 15, 2016 at 9:17:27 PM UTC+2, chobb...@gmail.com wrote: > > Thanks for the pointer for reshape. > > Sorry that I failed to make my question clear enough. In fact, I wonder if > there is a function which can _automatically_ recast a single-column > Array{T,2} to an Array{T,1} and do nothing if this Array{T,2} has multiple > rows. > > > > > > On Sunday, May 15, 2016 at 6:41:51 PM UTC+1, Tim Holy wrote: >> >> See reshape and vec. >> >> Best, >> --Tim >> >> On Sunday, May 15, 2016 09:00:33 AM chobb...@gmail.com wrote: >> > Here is rookie question, which I have tried to find a similar question >> and >> > answer in the history of this group, but failed. Please bear me. >> > >> > Since Julia distinguishes between Array{Float64,1} and >> Array{Float64,2}, >> > even there is only one column in an Array{Float64,2}. Therefore, the >> type >> > of rand(5) and rand(5,1) are not same. >> > >> > julia> x = rand(5) >> > 5-element Array{Float64,1}: >> > 0.183247 >> > 0.480208 >> > 0.623144 >> > 0.342792 >> > 0.0400695 >> > >> > julia> y = rand(5, 1) >> > 5x1 Array{Float64,2}: >> > 0.553224 >> > 0.0061835 >> > 0.762059 >> > 0.643796 >> > 0.655135 >> > >> > >> > Is there any way that I can upgrade x about to become an >> Array{Float64,2}? >> > And is there a way to downgrade y to become an Array{Float64,1}, when >> it >> > has only one column? >> > >> > In case that there is a similar discussion before, a link to the >> previous >> > thread is good enough and appreciated! >> > >> > Thanks! >> >>
Re: [julia-users] upgrade/downgrade between Array{Float64,1} and Array{Float64,2}
Thanks for the pointer for reshape. Sorry that I failed to make my question clear enough. In fact, I wonder if there is a function which can _automatically_ recast a single-column Array{T,2} to an Array{T,1} and do nothing if this Array{T,2} has multiple rows. On Sunday, May 15, 2016 at 6:41:51 PM UTC+1, Tim Holy wrote: > > See reshape and vec. > > Best, > --Tim > > On Sunday, May 15, 2016 09:00:33 AM chobb...@gmail.com > wrote: > > Here is rookie question, which I have tried to find a similar question > and > > answer in the history of this group, but failed. Please bear me. > > > > Since Julia distinguishes between Array{Float64,1} and Array{Float64,2}, > > even there is only one column in an Array{Float64,2}. Therefore, the > type > > of rand(5) and rand(5,1) are not same. > > > > julia> x = rand(5) > > 5-element Array{Float64,1}: > > 0.183247 > > 0.480208 > > 0.623144 > > 0.342792 > > 0.0400695 > > > > julia> y = rand(5, 1) > > 5x1 Array{Float64,2}: > > 0.553224 > > 0.0061835 > > 0.762059 > > 0.643796 > > 0.655135 > > > > > > Is there any way that I can upgrade x about to become an > Array{Float64,2}? > > And is there a way to downgrade y to become an Array{Float64,1}, when it > > has only one column? > > > > In case that there is a similar discussion before, a link to the > previous > > thread is good enough and appreciated! > > > > Thanks! > >
Re: [julia-users] upgrade/downgrade between Array{Float64,1} and Array{Float64,2}
See reshape and vec. Best, --Tim On Sunday, May 15, 2016 09:00:33 AM chobbes...@gmail.com wrote: > Here is rookie question, which I have tried to find a similar question and > answer in the history of this group, but failed. Please bear me. > > Since Julia distinguishes between Array{Float64,1} and Array{Float64,2}, > even there is only one column in an Array{Float64,2}. Therefore, the type > of rand(5) and rand(5,1) are not same. > > julia> x = rand(5) > 5-element Array{Float64,1}: > 0.183247 > 0.480208 > 0.623144 > 0.342792 > 0.0400695 > > julia> y = rand(5, 1) > 5x1 Array{Float64,2}: > 0.553224 > 0.0061835 > 0.762059 > 0.643796 > 0.655135 > > > Is there any way that I can upgrade x about to become an Array{Float64,2}? > And is there a way to downgrade y to become an Array{Float64,1}, when it > has only one column? > > In case that there is a similar discussion before, a link to the previous > thread is good enough and appreciated! > > Thanks!
[julia-users] upgrade/downgrade between Array{Float64,1} and Array{Float64,2}
Here is rookie question, which I have tried to find a similar question and answer in the history of this group, but failed. Please bear me. Since Julia distinguishes between Array{Float64,1} and Array{Float64,2}, even there is only one column in an Array{Float64,2}. Therefore, the type of rand(5) and rand(5,1) are not same. julia> x = rand(5) 5-element Array{Float64,1}: 0.183247 0.480208 0.623144 0.342792 0.0400695 julia> y = rand(5, 1) 5x1 Array{Float64,2}: 0.553224 0.0061835 0.762059 0.643796 0.655135 Is there any way that I can upgrade x about to become an Array{Float64,2}? And is there a way to downgrade y to become an Array{Float64,1}, when it has only one column? In case that there is a similar discussion before, a link to the previous thread is good enough and appreciated! Thanks!