Creation of a cell array with an iteration on an array

2 ビュー (過去 30 日間)
luca
luca 2019 年 10 月 15 日
回答済み: SaiDileep Kola 2020 年 12 月 24 日
SPI=[1 2 3 4 5 6 7 8 9];
GGG= {[1 2 1 2 1 1 1 2 3 4 4 5 4 5 5 4 4 5 5 4 6 6 6 6 6 6 3 3 9 9 3 1 9 9; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 ],[1 1 1 1 1 2 3 2 2 3 4 4 4 5 5 4 4 5 4 6 3 6 6 3 6 3 6 3 3 9 3 9 9 9 9; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175]};
tt = [20 20 20 20 20 20 20 20 20];
resultfin = {{[1 1 1 1; 5 10 20 30],[2 2 2 2; 10 20 50 60],[3 3 3 3 3 ; 10 30 40 50 60],[4 4 4 4 4 4; 20 40 50 60 70 80],[5 5 5; 20 40 70],[ 9 9 9 9 ; 60 70 80 90]},{[1 1 1 1; 5 10 20 30],[2 2 2 2; 10 20 50 60],[3 3 3 3 3 ; 10 30 40 50 60],[4 4 4 4 4 4; 20 40 50 60 70 80],[5 5 5; 20 40 70],[ 9 9 9 9 ; 60 70 80 90]}};
result = cell(size(GGG));
for gidx = 1:numel(GGG)
[~, ids] = ismember(GGG{gidx}(1, :), SPI);
[maxval,colNum] = max(GGG{gidx}(1, :)==SPI(:),[],2);
loc1 = maxval .* colNum;
count = accumarray(ids(:), 1)';
result{gidx} = arrayfun(@(v, s, n, t) [repelem(v, n); s + t.*(1:n)], SPI, GGG{gidx}(2, max(loc1,1)), count, tt(SPI), 'UniformOutput', false);
end
For each cell of GGG, The previous code is able to take the elements contained in the first raw of G as well as its correspondent value in the second raw of GGG. The first time we meet a diversity (where the possible diversity are in SPI) we take the correspondent value in the second raw of GGG and sum to it the correspondent value in tt.
Consider element 1 for the first cell of G: 1 appears with 5 in the second raw. So in the new cell “result” I want to have 1 and 25. For the second one that appear I want to put 1 and the value of the previous that is 25 plus again the value contained in tt. And the same for the following and each different diversity.
So the code now allows to obtain:
Now I want to modify the code according to two rules:
1) The first value of the second raw in result is always the same. That is, the first time we meet a diversity we take the correspondent value in the second raw of GGG and sum to it the correspondent value in tt. For 1 in the first cell is still 25.
2) For each cell and diversity, after put the first value as indicated in 1, I want to made a check to decide the value in the second raw of result that I want to put under each diversity. Consider element 1: The following one is at 15 in GGG{1,1}. Now let’s compare this value (15) with 25 (the previous value in the second raw) . If 15<(25+20) (where 20 is the value in tt) I want to insert 25+20=45. But imagine that instead of 15 we found 60, in this case 60>25+20 and in this case I want to put 60+20=70.Furthermore, for the second case I want also to create a cell array call “delay” where I put the difference between the values I use for comparison, every time condition two is satisfied.
To better understand: Let’s consider the last one in the first cell of GGG. It appears with a value below of 160. The previous one in result has value 105. Now, 160>105+20. So in “result” I want“ to put 160+20=180. And also insert in a new cell call “delay” the value 180-105. So for each diversity, every time the value under the first that I meet is higher then the previous plus the value in tt, I want to report that difference in the cell “delay”. Cell delay for each cell of GGG contain in the first raw the diversities and below each diversity the sum of those difference every time the condition is satisfied.
May someone help me with this difficult code or with other solution? I will be seriously grateful

回答 (1 件)

SaiDileep Kola
SaiDileep Kola 2020 年 12 月 24 日
Hi Luca,
You can refer to the examples given here which makes it easier to create cell array looping over an array.

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by