I have an array of arrays:
a=Array[[1,2,3,4],[11,12,13,14],[21,22,23,24]]
How can I get second and third element of each array?
[[2,3],[12,13],[22,23]]
I have tried it but it does not work:
a[:,2:3]
I have an array of arrays:
a=Array[[1,2,3,4],[11,12,13,14],[21,22,23,24]]
How can I get second and third element of each array?
[[2,3],[12,13],[22,23]]
I have tried it but it does not work:
a[:,2:3]
getindex.(a,2)
gets the second element of each array
You can also do reduce(hcat, a)[2:3,:]
, but that copies all the data into a matrix before extracting the relevant rows
Here are two ways to do this:
reduce(hcat, getindex.(a, Ref(2:3)) )
getindex.(a, (2:3)')
Ref(2:3)
means that this behaves like a scalar as far as broadcasting is concerned. Transposing it instead means that the 2nd axis of the result comes from 2:3
, and the first from a
.
This one is a keeper!
Thanks to your help, I have solved my problem.
Regards,