For loop in a cell array

조회 수: 7 (최근 30일)
car
car 2018년 7월 11일
이동: Voss 2023년 12월 14일
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
dpb
dpb 2018년 7월 11일
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 2018년 7월 12일
@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.

댓글을 달려면 로그인하십시오.

채택된 답변

car
car 2018년 7월 12일
이동: Voss 2023년 12월 14일
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!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by