0

I have a cell array (16x5) and I would like to extract all the values held in each column of the cell array and place them into a column within a matrix such that the columns are preserved (i.e. new matrix column for each cell array column).

What is the best way to do this?

I have tried:

for k=1:Samples
data(k,:) = [dist{:,k}];
end

But this returns the error

Subscripted assignment dimension mismatch.

However I am not sure why.

EDIT - Cell array structure: enter image description here

3
  • 2
    What is the content of your cell array elements (scalars, double arrays, etc)? Commented Feb 25, 2015 at 17:06
  • 1
    How can you check the data type of a cell array? 'whos' only defines it as a cell. I've updated the OP to include a screenshot of the cell array. Commented Feb 25, 2015 at 17:11
  • The output must be a cell array, right? Because otherwise those irregular shaped cell elements won't fit into a numeric array. And if, the output has to be a cell array, then it would be a 1 x 5 sized cell array, right? Commented Feb 25, 2015 at 19:35

1 Answer 1

1

Since your loop code is valid, I assume the error is being raised because data is preallocated with dimensions not matching the length of the comma-expanded dist column (Matlab will grow matrices with explicit indices but not with the : operator). You just need to get the length of the data after the comma-separated expansion:

nElem   = numel([dist{:,1}]);
Samples = size(dist,2);
data    = zeros(Samples,nElem);  
for k=1:Samples
  data(k,:) = [dist{:,k}];
end

Or if you want it in columns

data = zeros(nElem,Samples);  
for k=1:Samples
  data(:,k) = [dist{:,k}]';
end
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This was perfect and very insightful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.