[julia-users] Re: python a[range(x), y] equivalent in Julia
intuitive and more concise than a for loop. Thanks Dan. On Sunday, December 20, 2015 at 1:30:43 AM UTC-8, Dan wrote: > > Is the following red expression a good Julia equivalent: > > > julia> a = [ -1 2; 3 -4 ] > > ⋮ > > julia> y = [ 1, 2 ] > > ⋮ > > julia> [a[i,y[i]] for i=1:2] > 2-element Array{Any,1}: > -1 > -4 > > (it's also fast) > > On Sunday, December 20, 2015 at 2:44:20 AM UTC+2, Lex wrote: >> >> I am expecting [-1 -4] in the Julia example. >> >> On Saturday, December 19, 2015 at 4:42:07 PM UTC-8, Lex wrote: >>> >>> Hi >>> >>> In Python, I am able to select values like this: >>> >>> >>> a = np.matrix([[-1,2],[3,-4]]) >>> >>> y = np.matrix([1,0]) >>> >>> a[range(2),y] >>> matrix([[2, 3]]) >>> >>> >>> >>> Is there any equivalent in Julia or using a loop is the only way? >>> >>> julia> a >>> 2x2 Array{Int64,2}: >>> -1 2 >>> 3 -4 >>> >>> julia> y >>> 1x2 Array{Int64,2}: >>> 1 2 >>> >>> julia> a[:, y] >>> ERROR: MethodError: `index_shape_dim` has no method matching >>> index_shape_dim(::Array{Int64,2}, ::Int64, ::Array{Int64,2}) >>> >>> You might have used a 2d row vector where a 1d column vector was >>> required. >>> Note the difference between 1d column vector [1,2,3] and 2d row vector >>> [1 2 3]. >>> You can convert to a column vector with the vec() function. >>> Closest candidates are: >>> index_shape_dim(::Any, ::Any, ::Real...) >>> index_shape_dim(::Any, ::Any, ::Colon) >>> index_shape_dim(::Any, ::Any, ::Colon, ::Any, ::Any...) >>> ... >>> in getindex at abstractarray.jl:488 >>> >>> julia> a[:, y[]] >>> 2-element Array{Int64,1}: >>> -1 >>> 3 >>> >>> julia> a[:, y[:]] >>> 2x2 Array{Int64,2}: >>> -1 2 >>> 3 -4 >>> >>> julia> >>> >>> >>>
[julia-users] Re: python a[range(x), y] equivalent in Julia
Thanks Greg, seems like in this case y has to be a column vector else you get an error: julia> y 1x2 Array{Int64,2}: 1 2 julia> i = sub2ind(size(a), 1:2, y) ERROR: MethodError: `sub2ind` has no method matching sub2ind(::Tuple{Int64,Int64}, ::UnitRange{Int64}, ::Array{Int64,2}) You might have used a 2d row vector where a 1d column vector was required. Note the difference between 1d column vector [1,2,3] and 2d row vector [1 2 3]. You can convert to a column vector with the vec() function. Closest candidates are: sub2ind{T<:Integer}(::Tuple{Vararg{Integer}}, ::AbstractArray{T<:Integer,1}...) sub2ind(::Tuple{Vararg{Integer}}) sub2ind(::Tuple{Vararg{Integer}}, ::Integer...) julia> y = [1,2] 2-element Array{Int64,1}: 1 2 julia> i = sub2ind(size(a), 1:2, y) 2-element Array{Int64,1}: 1 4 On Saturday, December 19, 2015 at 11:51:11 PM UTC-8, Greg Plowman wrote: > > I'm guessing you want y to specify a column from each row. > Not sure how to do this directly. Closest I can think of this: > > a = [ -1 2; 3 -4 ] > y = [ 1, 2 ] > i = sub2ind(size(a), 1:2, y) > a[i] > >
Re: [julia-users] Re: python a[range(x), y] equivalent in Julia
Greg got it right, I am looking to use y to specify the columns to select in the matrix a. On Saturday, December 19, 2015 at 10:17:21 PM UTC-8, Stefan Karpinski wrote: > > In your example y is a row matrix, rather than a vector. If you make it a > vector it works: > > julia> a = [-1 2 > 3 -4] > 2x2 Array{Int64,2}: > -1 2 > 3 -4 > > julia> y = [1, 2] > 2-element Array{Int64,1}: > 1 > 2 > > julia> a[:,y] > 2x2 Array{Int64,2}: > -1 2 > 3 -4 > > > This seems like it may not be what you want though. Can you explain what > you'd like it to do? > > On Sat, Dec 19, 2015 at 7:44 PM, Lex > > wrote: > >> I am expecting [-1 -4] in the Julia example. >> >> >> On Saturday, December 19, 2015 at 4:42:07 PM UTC-8, Lex wrote: >>> >>> Hi >>> >>> In Python, I am able to select values like this: >>> >>> >>> a = np.matrix([[-1,2],[3,-4]]) >>> >>> y = np.matrix([1,0]) >>> >>> a[range(2),y] >>> matrix([[2, 3]]) >>> >>> >>> >>> Is there any equivalent in Julia or using a loop is the only way? >>> >>> julia> a >>> 2x2 Array{Int64,2}: >>> -1 2 >>> 3 -4 >>> >>> julia> y >>> 1x2 Array{Int64,2}: >>> 1 2 >>> >>> julia> a[:, y] >>> ERROR: MethodError: `index_shape_dim` has no method matching >>> index_shape_dim(::Array{Int64,2}, ::Int64, ::Array{Int64,2}) >>> >>> You might have used a 2d row vector where a 1d column vector was >>> required. >>> Note the difference between 1d column vector [1,2,3] and 2d row vector >>> [1 2 3]. >>> You can convert to a column vector with the vec() function. >>> Closest candidates are: >>> index_shape_dim(::Any, ::Any, ::Real...) >>> index_shape_dim(::Any, ::Any, ::Colon) >>> index_shape_dim(::Any, ::Any, ::Colon, ::Any, ::Any...) >>> ... >>> in getindex at abstractarray.jl:488 >>> >>> julia> a[:, y[]] >>> 2-element Array{Int64,1}: >>> -1 >>> 3 >>> >>> julia> a[:, y[:]] >>> 2x2 Array{Int64,2}: >>> -1 2 >>> 3 -4 >>> >>> julia> >>> >>> >>> >
[julia-users] Re: python a[range(x), y] equivalent in Julia
You can write a[[1,2], [2,1]] in Julia, but it means something different from what it (apparently) means in numpy. The meaning in Julia is equivalent to the Matlab meaning. To achieve what you seem to want you can use sub2ind: *julia> **a[sub2ind(size(a), [1,2], [2,1])]* *2-element Array{Int64,1}:* * 2* * 3*
[julia-users] Re: python a[range(x), y] equivalent in Julia
Is the following red expression a good Julia equivalent: julia> a = [ -1 2; 3 -4 ] ⋮ julia> y = [ 1, 2 ] ⋮ julia> [a[i,y[i]] for i=1:2] 2-element Array{Any,1}: -1 -4 (it's also fast) On Sunday, December 20, 2015 at 2:44:20 AM UTC+2, Lex wrote: > > I am expecting [-1 -4] in the Julia example. > > On Saturday, December 19, 2015 at 4:42:07 PM UTC-8, Lex wrote: >> >> Hi >> >> In Python, I am able to select values like this: >> >> >>> a = np.matrix([[-1,2],[3,-4]]) >> >>> y = np.matrix([1,0]) >> >>> a[range(2),y] >> matrix([[2, 3]]) >> >>> >> >> Is there any equivalent in Julia or using a loop is the only way? >> >> julia> a >> 2x2 Array{Int64,2}: >> -1 2 >> 3 -4 >> >> julia> y >> 1x2 Array{Int64,2}: >> 1 2 >> >> julia> a[:, y] >> ERROR: MethodError: `index_shape_dim` has no method matching >> index_shape_dim(::Array{Int64,2}, ::Int64, ::Array{Int64,2}) >> >> You might have used a 2d row vector where a 1d column vector was required. >> Note the difference between 1d column vector [1,2,3] and 2d row vector [1 >> 2 3]. >> You can convert to a column vector with the vec() function. >> Closest candidates are: >> index_shape_dim(::Any, ::Any, ::Real...) >> index_shape_dim(::Any, ::Any, ::Colon) >> index_shape_dim(::Any, ::Any, ::Colon, ::Any, ::Any...) >> ... >> in getindex at abstractarray.jl:488 >> >> julia> a[:, y[]] >> 2-element Array{Int64,1}: >> -1 >> 3 >> >> julia> a[:, y[:]] >> 2x2 Array{Int64,2}: >> -1 2 >> 3 -4 >> >> julia> >> >> >>
[julia-users] Re: python a[range(x), y] equivalent in Julia
I'm guessing you want y to specify a column from each row. Not sure how to do this directly. Closest I can think of this: a = [ -1 2; 3 -4 ] y = [ 1, 2 ] i = sub2ind(size(a), 1:2, y) a[i]
Re: [julia-users] Re: python a[range(x), y] equivalent in Julia
In your example y is a row matrix, rather than a vector. If you make it a vector it works: julia> a = [-1 2 3 -4] 2x2 Array{Int64,2}: -1 2 3 -4 julia> y = [1, 2] 2-element Array{Int64,1}: 1 2 julia> a[:,y] 2x2 Array{Int64,2}: -1 2 3 -4 This seems like it may not be what you want though. Can you explain what you'd like it to do? On Sat, Dec 19, 2015 at 7:44 PM, Lex wrote: > I am expecting [-1 -4] in the Julia example. > > > On Saturday, December 19, 2015 at 4:42:07 PM UTC-8, Lex wrote: >> >> Hi >> >> In Python, I am able to select values like this: >> >> >>> a = np.matrix([[-1,2],[3,-4]]) >> >>> y = np.matrix([1,0]) >> >>> a[range(2),y] >> matrix([[2, 3]]) >> >>> >> >> Is there any equivalent in Julia or using a loop is the only way? >> >> julia> a >> 2x2 Array{Int64,2}: >> -1 2 >> 3 -4 >> >> julia> y >> 1x2 Array{Int64,2}: >> 1 2 >> >> julia> a[:, y] >> ERROR: MethodError: `index_shape_dim` has no method matching >> index_shape_dim(::Array{Int64,2}, ::Int64, ::Array{Int64,2}) >> >> You might have used a 2d row vector where a 1d column vector was required. >> Note the difference between 1d column vector [1,2,3] and 2d row vector [1 >> 2 3]. >> You can convert to a column vector with the vec() function. >> Closest candidates are: >> index_shape_dim(::Any, ::Any, ::Real...) >> index_shape_dim(::Any, ::Any, ::Colon) >> index_shape_dim(::Any, ::Any, ::Colon, ::Any, ::Any...) >> ... >> in getindex at abstractarray.jl:488 >> >> julia> a[:, y[]] >> 2-element Array{Int64,1}: >> -1 >> 3 >> >> julia> a[:, y[:]] >> 2x2 Array{Int64,2}: >> -1 2 >> 3 -4 >> >> julia> >> >> >>
[julia-users] Re: python a[range(x), y] equivalent in Julia
I am expecting [-1 -4] in the Julia example. On Saturday, December 19, 2015 at 4:42:07 PM UTC-8, Lex wrote: > > Hi > > In Python, I am able to select values like this: > > >>> a = np.matrix([[-1,2],[3,-4]]) > >>> y = np.matrix([1,0]) > >>> a[range(2),y] > matrix([[2, 3]]) > >>> > > Is there any equivalent in Julia or using a loop is the only way? > > julia> a > 2x2 Array{Int64,2}: > -1 2 > 3 -4 > > julia> y > 1x2 Array{Int64,2}: > 1 2 > > julia> a[:, y] > ERROR: MethodError: `index_shape_dim` has no method matching > index_shape_dim(::Array{Int64,2}, ::Int64, ::Array{Int64,2}) > > You might have used a 2d row vector where a 1d column vector was required. > Note the difference between 1d column vector [1,2,3] and 2d row vector [1 > 2 3]. > You can convert to a column vector with the vec() function. > Closest candidates are: > index_shape_dim(::Any, ::Any, ::Real...) > index_shape_dim(::Any, ::Any, ::Colon) > index_shape_dim(::Any, ::Any, ::Colon, ::Any, ::Any...) > ... > in getindex at abstractarray.jl:488 > > julia> a[:, y[]] > 2-element Array{Int64,1}: > -1 > 3 > > julia> a[:, y[:]] > 2x2 Array{Int64,2}: > -1 2 > 3 -4 > > julia> > > >