How do a cell2mat conversion for a non-uniform cell to a matrix

조회 수: 14 (최근 30일)
Cutie
Cutie 2021년 9월 22일
댓글: Cutie 2021년 9월 28일
  1. I have a 1 x 10 cell with Non-uniform data in each cell array.
  2. How do a cell2mat conversion without losing any of the element.
  3. The output i want is such that the content of each of the cell is made a row vector with the subsequent cells 'concatenate' next to it in the order they appear in the cell array, i.e number of row of the matrix is equal to the max(length) while others are zero padded and the number of column is equal to number of column of the cell array. As in the example where the cell is 1x10, the result cell2mat matrix should be x-row by 10 column.
The shape looks like:
DD = {3401×1 double} {3601×1 double} {8081×1 double} {4041×1 double} {8721×1 double} {4281×1 double}
{5521×1 double} {4041×1 double} {4881×1 double} {5761×1 double}
From the above, the number of row in cell each array is different but I want an output that will covert the cell to a matrix.
Desire output
DD_matrix = 8721-row by 10-column (8721 being the cell with largest row number, others that follow should be zero padded)

채택된 답변

Simon Chan
Simon Chan 2021년 9월 22일
Do it in several steps as follows:
idx.size = cellfun(@length,DD);
idx.padded = max(idx.size)-idx.size;
DDpadded = cellfun(@(x,y) [x;zeros(y,1)],DD,num2cell(idx.padded),'uni',0);
DD_matrix = cell2mat(DDpadded);
  댓글 수: 3
Simon Chan
Simon Chan 2021년 9월 28일
You may refer to the MATLAB documentation about function cellfun.
In your case, there are two input arguments which are variable DD and num2cell(idx.padded).
So x and y are referring to variable DD and num2cell(idx.padded) respectively.

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

추가 답변 (1개)

Matt J
Matt J 2021년 9월 22일
cell2mat(DD(:))
  댓글 수: 1
Cutie
Cutie 2021년 9월 22일
편집: Cutie 2021년 9월 22일
Thank you @Matt J but this is not the output that I want.
The output i want is such that the content of each of the cell is made a row vector with the subsequent cells 'concatenate' next to it in the order they appear in the cell array, i.e number of row of the matrix is equal to the max(length) while others are zero padded and the number of column is equal to number of column of the cell array. As in the example where the cell is 1x10, the result cell2mat matrix should be x-row by 10 column.
The shape looks like:
DD = {3401×1 double} {3601×1 double} {8081×1 double} {4041×1 double} {8721×1 double} {4281×1 double}
{5521×1 double} {4041×1 double} {4881×1 double} {5761×1 double}
From the above, the number of row in cell each array is different but I want an output that will covert the cell to a matrix.
Desire output
DD_matrix = 8721-row by 10-column (8721 being the cell with largest row number, others that follow should be zero padded)

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by