Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Reshaping cell array having element with different sizes

Asked by AP on 15 Jun 2011

I have a variable named A which is a 5000×4 cell array containing M×2 matrices. M varies for each element. 5000 is the number of different measurement for 4 profiles. The following shows only two out of 5000 measurements. First row shows measurement#1 and the second row is measurement#2:

     [3413x2 double]    [3082x2 double]    [3186x2 double]    [3143x2 double]
     [3344x2 double]    [3044x2 double]    [3200x2 double]    [3143x2 double]

I am trying to have all the profile for each measurement in one matrix of size N×2, named B. for example, for the first measurement N=3413+3082+318+3143=12824. So, B is vertcat of the first row of A, it means B is constructed by putting each profile under the previous profile. finally B{i} will be all the profiles for ith measurement.

Could you help me with that?

0 Comments

AP

Tags

Products

No products are associated with this question.

2 Answers

Answer by Andrei Bobrov on 15 Jun 2011
Accepted answer
A1 = A';
B = arrayfun(@(x)cat(1,A1{:,x}),1:size(A1,2),'un',0)

3 Comments

AP on 15 Jun 2011

Thanks Andrei. This is so cool. I would really appreciate it if you could explain the second command in English.

AP on 15 Jun 2011

The result is the following:
A1 =
[3413x2 double] [3344x2 double]
[3082x2 double] [3044x2 double]
[3186x2 double] [3200x2 double]
[3143x2 double] [3143x2 double]
B =
[12824x2 double] [12731x2 double]

Andrei Bobrov on 16 Jun 2011

Hi! Having seen Your solution to <http://www.mathworks.com/matlabcentral/answers/9600-subtracting-a-constant-from-all-the-array-elements-in-a-structure>, think that the explanation is not needed?

Andrei Bobrov
Answer by Walter Roberson on 15 Jun 2011

I think this might work:

B = arrayfun(@(idx) [A{idx,:}], 1:size(A,1), 'un', 0);

3 Comments

AP on 15 Jun 2011

I get the following message:

??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

Error in ==> @(idx)[A{idx,:}]

AP on 15 Jun 2011

Another question that I have is that why did you use arrayfun while A is a cell array? Can't cellfun do the same?

Walter Roberson on 15 Jun 2011

Ah, try

B = arrayfun(@(idx) vertcat(A{idx,:}), 1:size(A,1), 'un', 0);

You use arrayfun instead of cellfun because cellfun processes each cell individually whereas you want to process groups of cells.

Walter Roberson

Contact us