Amir Kazemi
Amir Kazemi

Reputation: 131

Extracting a block out of a multidimensional matrix

Consider the multi-dimensional matrix A where size(A) has the identical even elements N. How should one find the matrix B with size(B)=size(A)/2 such that:

B(1,1,...,1)=A(1,1,...,1),
B(1,1,...,2)=A(1,1,...,2),
...
B(N/2,N/2,...,N/2)=A(N/2,N/2,...,N/2).

Upvotes: 2

Views: 65

Answers (1)

chappjc
chappjc

Reputation: 30579

I generally don't like arrayfun (or loopy functions), but if the number of dimensions is not in the thousands, then this should be just fine:

Nv = size(A)/2;
S = arrayfun(@(x){1:x},Nv);
B = A(S{:});

Should work with different sized dimensions too. Just decide how you want to deal with dimensions where mod(size(A),2)~=0.

Upvotes: 4

Related Questions