Question

I have a structure like this one:

>> A = [1 2; 3 4];
>> B = [5 6; 7 8];
>> C = [9 10; 11 12];
>> D = [13 14; 15 16];
>> S = {A B; C D}

S = 

[2x2 double]    [2x2 double]
[2x2 double]    [2x2 double]

I wonder if there is any way to extract the first element of each array within the cell array by using just one command. For example, I would like to get

ans = [S{1,1}(1) S{1,2}(1); S{2,1}(1) S{2,2}(2)]

ans =

 1     5
 9    15

but automatically and in a programmable way, i.e. using colons or similar.

Loops are not desired.

Is it possible?

Thank you.

Was it helpful?

Solution

You can use cellfun :

FirstElement = cellfun(@(x)x(1),S)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top