For example:
a = Range[20];
a = ArrayReshape[a, {4, 5}]
I want to take elements from a
with interval = 2 :
Take[a, {1, -1, 2}]
fails to do this.
It would be better if it works for 3D or higher-dimensional large matrix.
That is precisely what Downsample
is for:
a = Range[20];
a = ArrayReshape[a, {4, 5}];
Downsample[a, 2]
(* {{1, 3, 5}, {11, 13, 15}} *)
a[[;; ;; 2, ;; ;; 3]] == Downsample[a, {2, 3} ]
. Actually, using Part
seems to me slightly more flexible, because different offsets can be specified for each direction (e.g. a[[ hoff ;; ;; 2, voff ;; ;; 3 ]]
, but maybe I misunderstand Downsample
. The Downsample
syntax is more readable than the Part
specification though.
$\endgroup$
offset
can be a list: Downsample[Partition[Range[20], 5], {2, 3}, {1, 2}]
gives the expected result.
$\endgroup$
Commented
May 2, 2015 at 20:01
Take[Partition[Range[20], 5], {1, -1, 2}, {1, -1, 2}]
works, tho. Probably more generally:myTake[list_?ArrayQ] := Take[list, Sequence @@ ConstantArray[{1, -1, 2}, ArrayDepth[list]]]
. (I don't have Mathematica on hand for testing.) $\endgroup$a[[;; ;;2, ;; ;; 2]]
(look upPart
andSpan
) $\endgroup$