user2372976
user2372976

Reputation: 725

Extract elements from matrix

How can I extract the elements: [1,2,5,6], [3,4,7,8], [9,10,13,14], [11,12,15,16] ?

A = [1,    2,    3,    4;
     5,    6,    7,    8;
     9,    10,   11,  12;
     13,   14,   15,  16;];

I'm using octave.

Best regards, Chris.

Upvotes: 0

Views: 137

Answers (1)

Nishant
Nishant

Reputation: 2619

If you need four matrices then use

out = mat2cell(A,[2 2], [2 2]);

If you need four vectors with values , then use

out = cellfun(@(x)(reshape(x,1,[])),mat2cell(A,[2 2], [2 2]),'UniformOutput',0);

output will be

out{:,:}

ans =

     1     5     2     6


ans =

     9    13    10    14


ans =

     3     7     4     8


ans =

    11    15    12    16

Thanks, to Joe Serrano ,If you need the value in each of the four vectors in same order use,

out = cellfun(@(x)(reshape(x',1,[])),mat2cell(A,[2 2], [2 2]),'UniformOutput',0);

output will be

out{:,:}

ans =

     1     2     5     6


ans =

     9    10    13    14


ans =

     3     4     7     8


ans =

    11    12    15    16

Upvotes: 4

Related Questions