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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by