MATLAB Answers

Create an array referring to another array

1 view (last 30 days)
luca
luca on 7 Oct 2019
Edited: Adam Danz on 7 Oct 2019
Given the following code
clear all
clc
SP =[1 2 3 4 5 6 9]
G= {[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 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 ],[ 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 ]};
tt = [20 20 20 20 20 20 20 20 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, t) [repelem(v, n); s + t.*(1:n)], uval, G{gidx}(2, loc1), count, tt(uval), 'UniformOutput', false);
end
end
VM = cellfun(@(z)cellfun(@(x)sum(x(2,:)<= 120),z),result,'UniformOutput',false);
The code count how many 1-2-3-4-5-6-9 are present in "result" before a value of 120 is reach in each cell of result. For example, considering "result{1, 1}{1, 2}"
Cattura2.PNG
the number of 2 before 120 is reached in the second row is 3. so in VM we will have a 3.
If I run the code I obtain as a result :
Cattura.PNG
that means referring to "SP" that In "result{1, 1} " I have five 1, three 2, three 3, three 4, three 5, zero 6 and zero 9.
In case of "result{1, 2}" I dont have 1 inside and so " VM{1, 2}" has 6 element instead of 7. But since 1 is not inside I would like to have
0 3 5 4 3 2 0
instead of
3 5 4 3 2 0
May someone help me in order to modify the code and get the result?

Accepted Answer

Adam Danz
Adam Danz on 7 Oct 2019
Here's a list of some of the changes made to your code. The new version is below and it produces the outputs you described in your question.
  • The first for-loop in your code doesn't do anything useful. I just forces the code to do the same work twice so I removed it.
  • I replaced the unique() approach with an ismember() approach.
  • Instead of relying on the unique values in G{gidx}(1, :)), we're just using the values listed in SP.
SP =[1 2 3 4 5 6 9];
G= {[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 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 ],[ 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 ]};
tt = [20 20 20 20 20 20 20 20 20];
% for i = 1:size(G,2)
result = cell(size(G));
for gidx = 1:numel(G)
[~, ids] = ismember(G{gidx}(1, :), SP);
[maxval,colNum] = max(G{gidx}(1, :)==SP(:),[],2);
loc1 = maxval .* colNum;
count = accumarray(ids(:), 1)';
result{gidx} = arrayfun(@(v, s, n, t) [repelem(v, n); s + t.*(1:n)], SP, G{gidx}(2, max(loc1,1)), count, tt(SP), 'UniformOutput', false);
end
% end
VM = cellfun(@(z)cellfun(@(x)sum(x(2,:)<= 120),z),result,'UniformOutput',false);
Results
>> celldisp(VM)
VM{1} =
5 3 3 3 3 0 0
VM{2} =
0 3 5 4 3 2 0

More Answers (0)

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by