# loop over a matrix by taking the previous index

3 views (last 30 days)

Show older comments

hi,

i want to loop over a matrix and add the element to a row by taking the index of the previous element.

e.g.

first_element = find(gnbh(:,1) == 0 ) %now i have the first element i need

i = 1:length(first_element)

j = first_element(i)

next_element = gnbh(j,2)

next_element1 = gnbh(next_element,2)

next_element2 = gnbh(next_element1,2)

and so on ... until my next_element is 0

in the end i want to add to every element j the element next_element,next_element1,next_element2 ...

i hope somebody can help me

##### 9 Comments

Guillaume
on 20 Nov 2019

### Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!