For loop in a cell array

3 views (last 30 days)
car
car on 11 Jul 2018
Moved: Voss on 14 Dec 2023
I'm trying to convert data in a cell array to a matrix, but the dimensions are mismatched. I've found an anonymous function I can use to pad the arrays with NANs so that the cell2mat fuction will make the conversion. My problem now is applying the function to all the arrays without doing so one by one.
For some background, I have a 1x54 cell array. Cell {1,1} is a 1x8 array. The other 53 cells contain a similar array. The 1x8 array is what I have padded with NANs and converted to a matrix using the following function:
maxSize_mint = max(cellfun(@numel,master_mint{1,1})); %master_mint is the 1x54 cell array
out_mint=cell2mat(cellfun(@(x)cat(1,x,nan(maxSize-numel(x),1)),master_mint{1,1},'UniformOutput',false));
now my problem is trying to loop through the 1x54 array to do this to all cells. So far each way I try to do so gives me a different error.
Help please!
  4 Comments
dpb
dpb on 11 Jul 2018
Again, what is the expected output???
It doesn't make any sense from the description that if the first cell is a numeric vector and "The other 53 cells contain a similar array" there should be any issue whatsoever.
Instead of continuing to post nonworking code to try to debug; I suggest attach a small subset of the data (altho 8*54 isn't very big if that's really all there is) and then describe explicitly what the expected output should be.
Jan
Jan on 12 Jul 2018
@Carling Walsh: We want to help you. Please post a clear description.
  • What are the inputs?
master_mint is a {1 x 54} cell
master_mint{1,1} is 1x8 array - of which type?
What are the the other elements?
  • What is the wanted output?
A 8 x 54 numerical array padded with NaNs?
A {1 x 54} cell array, where the elements are padded with NaNs?
Please post a short but relevant example. The current descriptions are only useful to understand, that the posted code does not work.

Sign in to comment.

Accepted Answer

car
car on 12 Jul 2018
Moved: Voss on 14 Dec 2023
Hey guys, sorry my overall question ind information wasn't overly clear, my bad. I did want to update everyone that I solved my own problem, and to thank you for you interest! I'll show you what I did for reference.
Turns out I didn't need a for loop at all. Instead of:
out=cell2mat(cellfun(@(x)cat(1,x,nan(maxSize-numel(x),1)),master_mint{1,1},'UniformOutput',false));
I wrote it as:
out{k}=cell2mat(cellfun(@(x)cat(1,x,nan(maxSize-numel(x),1)),master_mint{k},'UniformOutput',false));
k refers to a larger for loop this function is in. This returned a new 1x54 cell array where each cell is a 120x8 double. What this does is allow me to run statistical tests using cellfun, whereas I couldn't before as each of the original 54 cells were a 1x8 cell. I hope that clears up all of the confusion from before, and thanks again for trying to help!

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!