One solution:

julia> hcat([B[:,k][E[:]] for k=1:size(B,2)]...)
10x5 Array{Int64,2}:
 11  18  25   2   9
 11  18  25   2   9
 11  18  25   2   9
 10  12  19  21   3
 11  18  25   2   9
 11  18  25   2   9
 11  18  25   2   9
 11  18  25   2   9
 17  24   1   8  15
 11  18  25   2   9


On Tuesday, June 10, 2014 6:23:35 AM UTC-7, Bruno Rodrigues wrote:
>
> Hi, I have a question concerning element selection in an array. Suppose 
> you have the following arrays:
>
> B = [17 24 1 8 15; 23 5 7 14 16;4 6 13 20 22; 10 12 19 21 3;11 18 25 2 9]
>
> E =[5     5     5     4     5     5     5     5     1     5]
>
> If I type:
>
> B[E]
>
> I get:
>
> 11  11  11  10  11  11  11  11  17  11
>
> The 5th element of B is 11, so since E  = 5, 5, 5... the first three 
> elements of B[E] is 11 11 11. Now, comes my question: is there a way to do 
> this for all columns of B? In Matlab, I would write something like: B(E,:) 
> and get
>
>
> >> B(E,:)       
>
> ans =
>
>     11    18    25     2     9
>     11    18    25     2     9
>     11    18    25     2     9
>     10    12    19    21     3
>     11    18    25     2     9
>     11    18    25     2     9
>     11    18    25     2     9
>     11    18    25     2     9
>     17    24     1     8    15
>     11    18    25     2     9
>
> but this doesn't work in Julia
>
> B[E,:]
> ERROR: no method getindex(Array{Int64,2}, Array{Int64,2}, UnitRange{Int64
> })
>
> so I need to write a loop:
>
> S = zeros(10,5)
> x = [1 2 3 4 5]
>
> for i in x
>        S[:,i] = B[:,i][E[end,:]]
>        end
>
> Is there a way to avoid this loop?
>
>
>

Reply via email to