Create arrays based on cell arrays

1 view (last 30 days)
luca
luca on 24 Sep 2019
Commented: luca on 7 Oct 2019
Hi,
given the following code
clear all
clc
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 ],[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];
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
I want to derive X arrays from the values contained in "result". The number x is equal to the number of cell contained in result
Each array should contain the number of times the different elements are repeated in each cell, but till we reach a value lower than 120 in the second raw of each cell.
Let's see better what I mean.
Consider the first cell
Cattura.PNG
so
Cattura2.PNG
now consider the first cell
Cattura3.PNG
I want to collect all the values of 1 that has a value in the second raw lower equal than 120. in this case N1=5
Considering the 5th cell
Cattura4.PNG
in this case N5=3, cause at the 4th value we find 140>120.
so the first array that we want to obtain for the first 1*7 cell of G will be
V1= [ 5 3 3 3 3 0 0]
May someone help me to get these arrays ?

Accepted Answer

Adam Danz
Adam Danz on 24 Sep 2019
V = cellfun(@(z)cellfun(@(x)sum(x(2,:)<=120),z),result,'UniformOutput',false);

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