loop over a matrix by taking the previous index

조회 수: 5 (최근 30일)
bbah
bbah 2019년 11월 20일
댓글: bbah 2019년 11월 20일
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
Guillaume
Guillaume 2019년 11월 20일
What is the significance of the number in the first column? It doesn't appear to have any meaning? other than maybe you want to start at every 0. The non-zero values of the first column don't appear to be used anywhere.
bbah
bbah 2019년 11월 20일
good question. the 0 in both columns actually stays for a different type of elements. that means for every 0 the neighbour element in +x or -x direction will be a completely different type of element which im not interested in. absolutely no neighbour would be -1.
what i am trying to do is to collect the elements by starting at -x = 0 ( which means my next element in -x direction is not my type of element) and add every element until i reach my last element ( which means my next element in x direction would be again a different element and it has the value +x=0)
with the loop i did in the beginning i can get the 1st elements that start at -x=0 and my next elements at +x but i dont know how to get from my next element the next element in +x direction until i reach +x=0.
i hope u can understand me

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by