cration of contents inside the cell array of uniform size

조회 수: 1 (최근 30일)
Hi,
In my loop operation I am generating 1 X 72 cell. At every iteration the values will be stored in the consequetive columns in the form of 8 x 3 double. However, in some cases it is 5 x3 or 2 x3. I would like to make all entries in the form of 8 x 3.. Could someone please help me with this ..
I have attached my matrix for an reference..

채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 15일
result = cellfun(@(C) full([(0:7).', sparse(C(:,[1 1])+1, [1*ones(1,size(C,1)), 2*ones(1,size(C,1))], C(:,[2 3]), 8, 2)]), Data, 'uniform', 0);
  댓글 수: 6
Walter Roberson
Walter Roberson 2021년 9월 18일
mask = cellfun(@isempty, Data));
result(mask) = {[(0:7).', zeros(8,2)]};
result(~mask) = cellfun(@(C) full([(0:7).', sparse(C(:,[1 1])+1, [1*ones(1,size(C,1)), 2*ones(1,size(C,1))], C(:,[2 3]), 8, 2)]), Data(~mask), 'uniform', 0);
Turbulence Analysis
Turbulence Analysis 2021년 9월 18일
Thanks a lot, Walter.. !!!!

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

추가 답변 (1개)

Simon Chan
Simon Chan 2021년 9월 15일
May try the following to add NaN to the new rows
load('data.mat');
addrow = cellfun(@(x) 8-size(x,1),Data,'uni',0);
result = cellfun(@(x,y) vertcat(x,NaN(y,3)),Data,addrow,'uni',0);
  댓글 수: 1
Turbulence Analysis
Turbulence Analysis 2021년 9월 15일
Hi,
Many thanks.. The only isssue is , in the 8 x 3 array the first columns should be always 0 1 2 3 4 5 6 7... For other 2, 3 columns NaN entries is not a problem..
Is there a way out??

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by