Modify a cell array referring to another cell array

조회 수: 1 (최근 30일)
luca
luca 2019년 9월 24일
댓글: luca 2019년 9월 24일
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
% ^^^......^^^
luca
luca 2019년 9월 24일
yes !

댓글을 달려면 로그인하십시오.

채택된 답변

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에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by