1

I have a cell that consists in many different arrays with different dimensions. For example, this could be my cell:

A = {[-1] [144x1 double] [-1] [331x1 double] [-1] [431x1 double] [-1] [512x1 double]}

I want to do a histogram with all the data contained in the cell. I've tried doing a cell2mat:

B = cell2mat(A);

in order to extract the data and then do the histogram, but it is giving this error:

??? Error using ==> cat
CAT arguments dimensions are not consistent.
Error in ==> cell2mat at 81
            m{n} = cat(2,c{n,:});
Error in ==> function at 156
B = cell2mat(A);

I also tried creating a matrix with a loop:

for j=1:length(A)

    B = [B; A{j}]

end

But the problem here is that MATLAB is using the same dimensions for all arrays and it is filling the empty spaces with 0.

How do I achieve this properly?

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96

1 Answers1

3

If all your cells contain matrices of size 1 x n and your cell array is also of size 1 x n then cell2mat will work. The same is true if all matrices are of size n x 1 and cell array is of size n x 1.

So in your given example try simply:

B = cell2mat(A');

More generically, the loop will work for a cell array with generic n x m contents with a slight modification:

B = [];
for n=1:length(A)    
    B = [B; A{n}(:)]
end
nkjt
  • 7,825
  • 9
  • 22
  • 28