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 で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!