loop over a matrix by taking the previous index
이전 댓글 표시
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
2019년 11월 20일
I don't really understand what you're trying to do. Your pseudocode doesn't make much sense. Note that:
i = 1:length(first_element)
j = first_element(i)
is the same as
j = first_element;
Could you give an example of an input matrix and the desired result?
Guillaume
2019년 11월 20일
You make it extremely hard to help you. Why can't you use valid matlab syntax to give us a complete example of output and inputs?
demoinput = [0 10; 0 5; 2 6; ? ?; ? ?; ? ?; ? ?; ? ?; ? ?; 1 5; ? ?]; %fill in the ?
desiredoutput = [1 10 ? ?]; %fill in the ?
Why is the first value of the desired output 1? Particularly as 0 is present in at least 2 rows.
bbah
2019년 11월 20일
Guillaume
2019년 11월 20일
bbah's comment originally posted as an answer moved here:
this would be my input for the first 34 rows
-x +x
0 10
0 11
0 12
0 13
0 14
0 15
0 16
0 17
0 0
1 18
2 19
3 20
4 21
5 22
6 0
7 23
8 24
10 25
11 26
12 27
13 28
14 29
16 30
17 31
18 32
19 33
20 34
21 35
22 36
23 38
24 39
25 41
26 42
27 44
0 = no neighbour in the -x or +x direction
the result i want is:
matrix =
Output = [1 10 18 25 41;2 11 26 42;3 12 27 44 ... ]
if there is no neighbour just add 0
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
2019년 11월 20일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!