Add empty cell inside a cell array considering a single array
조회 수: 23 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
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)
추가 답변 (2개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!