# Create an array iterating on another array

19 views (last 30 days)

Show older comments

Hi, given the arrays

SP = [1 2 3 4 5 6 9];

T = [10 5 15 5 10 15 20 15 10];

M = [5 6 7 8 10 4 2 ];

G= {[1 2 1 2 1 1 1 2 3 4 4 5 4 5 5 4 4 5 5 4 5 4 5 4 5 4 6 6 6 6 6 6 3 3 9 9 3 9 6 9 6 9 3 9 9 9 6 3 3 6 9 3 ; 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 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260],[ 2 3 2 2 3 2 4 5 3 3 4 4 4 5 5 4 4 5 5 3 4 6 3 6 5 3 6 3 3 4 6 6 3 6 3 3 9 3 9 9 6 9 9 3 6 9 9 3 9 9 9 6 3 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 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270]};

N= [5 3 8 9 8 10 9; 0 4 16 8 6 9 11 ]

SP indicated the number of diversity elements. T indicate a number that refers to each diversity (10 to 1, 5 to 2, 15 to 3 and so on). M indicate a number that refers to each diversity (5 to 1, 6 to 2, 7 to 3 and so on). G is the vector that we use to iterate. N has as many rows as the cell of G, and count the number of diversity that are contained in the first row of each cell.

Consider just a part of the first cell G{1, 1} (but I need to iterate on each cell):

I want to iterate on this vector and every time I meet a diversity element (indicated in SP) I want to do the following calculation.

M - length-value/T + N

Where M,T,N are the values corresponding to the diversity 1.

"lenght" is the final value of the second raw in G{1, :} the sequence, that is 260 in the first case.

"value" is the number still of the second raw of G{1, :} where I meet the first time the diversity.

For example, considering the element 1 the calculus will be

5 - (260-5)/10 + 5

considering element 3

7 - (260-45)/15 + 8

and so on.

N.B. considering G{1, 2}, where the diversity one is not included, the formula for one will be just

M - length/T + 0

May someone help me with this code ?

##### 0 Comments

### Accepted Answer

Guillaume
on 8 Oct 2019

Edited: Guillaume
on 8 Oct 2019

If I understood correctly:

out = zeros(size(N));

for row = 1:size(N, 1)

[isfound, where] = ismember(SP, G{row}(1, :)); %where will be the first column in G where SP is found, 0 otherwise

out(row, isfound) = M(isfound) - (G{row}(2, end) - G{row}(2, where(isfound))) ./ T(isfound) + N(row, isfound);

out(row, ~isfound) = M(~isfound) - G{row}(2, end) ./ T(~isfound);

end

##### 5 Comments

Guillaume
on 11 Oct 2019

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!