Modify a cell array referring to another cell array
显示 更早的评论
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
2019-9-24
The code in your question does not have a t9. Does t8 and t9 both equal 20?
Do you really need separate variables for t1,t2... tn? Can we just use a vector t and use indexing such as t(n)?
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
% ^^^......^^^
luca
2019-9-24
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!