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 commentaires

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?
bbah
bbah le 20 Nov 2019
Modifié(e) : bbah le 20 Nov 2019
i got a matrix which is 19313x2 called gnbh
from the first column i get my starting element. and the second column shows me the neighbour of my first element in x direction.
e.g. my starting element is 1 and the neighbour is 10. Now i want to loop over the matrix to get the neighbour of 10 and so on. in the end i want to have a row containing the neighbours [1,10 ...]. I want to loop until my next element is 0 and stop
i can get my first element and the neighbour (second element) like this but how do i loop to get the neighbour of my second and so on ?
first_element = find(gnbh(:,1) == 0 )
for i = 1:length(first_element)
j = first_element(i)
next_element = gnbh(j,2);
row = [j,next_element]
end
thank you
Stephen23
Stephen23 le 20 Nov 2019
@bbah: it would help a lot if you could also supply the requested data:
"Could you give an example of an input matrix and the desired result?"
bbah
bbah le 20 Nov 2019
Modifié(e) : bbah le 20 Nov 2019
my input would be e.g.
Matrix (0 10; 0 5; 2 6; ... the 10th row would be ;1 5)
like i said: my second column shows me the neighbour of my element (here for the 1st element it is the 10th element ) in +x direction
my first column is the neighbour in -x direction ( here for the 10th element it is my 1st element)
this would be my input matrix
my output would be a matrix with my first column as my first element and second column my 2nd element
output = (1 10 next neighbour next neighbour; ... )
i hope u can understand what im trying to do
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.
sorry
demoinput = [0 2;1 3; 2 4:3 0]
u see the 1st elements neighbour is the 10th element in +x direction. the -x neighbour of the 4th element is the 6th element and so on]
0 would be there is no neighbour
desiredoutput = [1 2 3 4 0; 2 3 4 0; 3 4 ? ?]
the problem here is i do not know how to put the neighbour of the next neighbour in a row and how to deal when no neighbour is there.
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
Guillaume le 20 Nov 2019
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 le 20 Nov 2019
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

Connectez-vous pour commenter.

Réponses (0)

Catégories

Question posée :

le 20 Nov 2019

Commenté :

le 20 Nov 2019

Community Treasure Hunt

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

Start Hunting!

Translated by