Add empty cell inside a cell array considering a single array

조회 수: 23 (최근 30일)
luca
luca 2019년 10월 17일
답변: Daniya Zafar 2022년 1월 5일
Hi I have a cell array
GGG = {{[1 2 2 1 3 4 9 9 6 1 3 3 2 1 2 4 3 ; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85],[ 2 2 3 3 4 9 4 9 6 4 9 3 3 2 2 4 ; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 78]},{[ 2 2 3 3 4 9 4 9 6 4 9 3 3 2 2 4 ; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 78]}};
given an array
SP= [1 2 3 4 5 6]
I want to add in each cell of GGG a number of empty cell that is equal to the maximum value inside SP minus the actual number of cell in each cell of GGG
CONSIDERING THE FIRST CELL OF GGG, is a 1*2 cell. inside this cell I want to add 6-2=4 empty cell
CONSIDERING THE SECOND CELL OF GGG, is a 1*1 cell, inside this cell I want to add 6-1=5 empty cell
obtaining
GGG = {{[1 2 2 1 3 4 9 9 6 1 3 3 2 1 2 4 3 ; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85],[ 2 2 3 3 4 9 4 9 6 4 9 3 3 2 2 4 ; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 78],[],[],[],[]},{[ 2 2 3 3 4 9 4 9 6 4 9 3 3 2 2 4 ; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 78],[],[],[],[],[]}};
May someone help me with this code?

채택된 답변

Guillaume
Guillaume 2019년 10월 17일
Surely, by now, with all the questions you've asked, you should be able to manipulate cell arrays yourself.
Anyway:
desiredlenght = max(SP);
result = cellfun(@(c) [c, cell(1, desiredlength - numel(c))], GGG, 'UniformOutput', false)
  댓글 수: 1
luca
luca 2019년 10월 18일
Unfortunately still I'm not familiar with the use of cellfun!
In any case thanks Guillaume for all your explanation, I think I've improved my MATLAB knowledge starting from the beginning

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

추가 답변 (2개)

Raptrick
Raptrick 2019년 10월 17일
Hi Luca,
Does this help you?
GGG = {{[1 2 2 1 3 4 9 9 6 1 3 3 2 1 2 4 3 ; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85],[ 2 2 3 3 4 9 4 9 6 4 9 3 3 2 2 4 ; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 78]},{[ 2 2 3 3 4 9 4 9 6 4 9 3 3 2 2 4 ; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 78]}};
SP= [1 2 3 4 5 6];
for i =1:length(GGG)
addEmptyCells = max(SP)-length(GGG{i});
for j = 1:addEmptyCells
GGG{i}{end+1} = [];
end
end
Patrick

Daniya Zafar
Daniya Zafar 2022년 1월 5일
add empty cells using {''}

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by