Add empty cell inside a cell array considering a single array

24 次查看(过去 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?

采纳的回答

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