Modify a cell array referring to another cell array

1 次查看(过去 30 天)
Given the following code
G= {[1 2 1 2 1 2 3 4 5 4 5 4 6 3 9 3 9; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85],[1 1 1 1 2 3 4 4 4 5 4 6 3 6 3 9; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 ]};
SP= [1 2 3 4 5 6 9];
t1 = 20; t2 = 20 ; t3 = 20 ; t4 = 20 ; t5 = 20 ; t6 = 20 ; t7 = 20; t8=20 ; t9=20;
for i = 1:size (G,2)
result = cell(size(G));
for gidx = 1:numel(G)
[uval, loc1, ids] = unique(G{gidx}(1, :));
count = accumarray(ids, 1)';
result{gidx} = arrayfun(@(v, s, n) [repelem(v, n); s, zeros(1, n-1)], uval, G{gidx}(2, loc1), count, 'UniformOutput', false);
end
AB = G{i}(1,:);
end
With the value contained in the fist raw of each cell G, I want to modify the arrays contained in cell "result"
I for example we take the first raw of the first cell of G, reported as AB in each cycle:
1 2 1 2 1 2 3 4 5 4 5 4 6 3 9 3 9
at each iteration everytime I meet a specified element, I want to modify its array in result.
In particular, if I consider result{1, 1}{1, 1}" that was
1 1 1
5 0 0
Should now become
1 1 1
5+t1 5+t1+t1 5+t1+t1+t1
so
1 1 1
25 45 65
That is: every time I meet 1 in AB, I want to modify the values inside "result" that refer to that element and add everytime the value t1 to the previous one.
For element 2, at the beginning the result was
2 2 2
10 0 0
Then has to become
2 2 2
10+t2 10+t2+t2 10+t2+t2+t2
...
For element 9, at the beginning the result was
9 9
75 0
Then has to become
9 9
75+t9 75+t9+t9
I hope that it's clear what I would like to obtain
May someone help me with this task?
  4 个评论
Adam Danz
Adam Danz 2019-9-24
编辑:Adam Danz 2019-9-24
The last example breaks your pattern. I think the last example should be
For element 9, at the beginning the result was
9 9
75 0
Then has to become
9 9
75+t9 75+t9+t9
% ^^^......^^^

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2019-9-24
编辑:Adam Danz 2019-9-24
Replace your t1,t2, t3... with this
tt = [20 20 20 20 20 20 20 20 20]; %Much easier to work with
Then replace the result{} line in the for(gidx...) loop with this
result{gidx} = arrayfun(@(v, s, n, t) [repelem(v, n); s + t.*(1:n)], uval, G{gidx}(2, loc1), count, tt(uval), 'UniformOutput', false);
% Changes > > > > > > > > > > > 1) ^ > > > > > > > 2) ^^^^^^^^^^ > > > > > > > > > > > > > > > 3) ^^^^^^^^

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by