Question about executing commands while true

1 回表示 (過去 30 日間)
Brian
Brian 2013 年 2 月 18 日
Hello, all, I have a simple question about executing a command while a statement is true. I will try to keep my explanations as clear and simple as possible.
A brief background of what I am doing is I have a matrix of connected points (I call this my connMx for future reference):
ex. 1 to 4
4 to 2
4 to 3
making a bifurcation, and I want to create a matrix of the number of terminal segments for each of these connections. In the above example, my numTerm vector would be:
2 1 1
since the first connection (1 to 4) has two terminal endpoints, and the second and third lines are endpoints.
As one more example, the connection matrix:
1 to 4
4 to 2
4 to 6
6 to 3
6 to 5
my numTerm matrix would be:
3 1 2 1 1
since 1 to 4 has 3 terminal endpoints (2, 3, 5), 4 to 2 has one terminal endpoint (2), 4 to 6 has 2 terminal endpoints (3, 5), and the last two are terminal segments.
Now, what I am trying to do is write code that numerates this automatically. I have a random binary tree builder, so my example matrices may be different in each case, but the way I determine numTerm is the same.
What I have now is a function:
getNumTerminalsNew(propMx, randomgen_connMx, bif_index)
where propMx is the matrix I contain my numTerm vector in, randomgen_connMx is my connection matrix, and bif_index is the index of the randomly added point. Since this is quasi-random, this differs each time I run the code, but it always refers to the last integer in the first column in the connection matrix (4 in the first case, 6 in the second case).
Another thing to note is this is a iterative process, meaning I create the first bifurcation (the first example), do some math, then add another segment (second example), do some more math, then continue the process. I already have a numTerm vector with each iteration, I merely add to it with each iteration.
What I do from here is find the connMx index values where the bif_index is in the first column (i.e. the last two entries) and set them to 1 (because they will always have 1 nTerm). Then I steadily go upstream, and find where bif_index is in the second column entry (meaning it leads to the terminal endpoint bifurcation), find that connMx index, and numerate the nTerm vector at that index by 1, and do this at the next upstream face all the way to the top of the connMx.
This is where my problem is. I try to do this with the command:
while find(bif_index == randomgen_connMx(:,2), 1) == 1
upstreamFace = find(bif_index == randomgen_connMx(:,2));
propMx(upstreamFace,4) = propMx(upstreamFace,4)+1;
bif_index = randomgen_connMx(bif_index,1);
end
but this command never executes, it glosses over it (which means the statement is never true?)
As a simple explanation: the first line determines if the bif_point exists in the second column (meaning its a point that leads to a bifurcation). The second line determines the index of the connMx that this happens at. The third line numerates the propMx numTerm vector where this happens, and the final line sets the bif_index to the first element of that row (and restarts the while loop).
At the top segment (1 to 4), the first line will fail because there is no second column entry for the integer 1. It only exists in the first column.
My problem reiterated is I have this code, but it never executes. It jumps over it every time. Is this a result of me coding unintelligently, or something else?
Thanks for your help in this, always learning new things in MATLAB.

採用された回答

Brian
Brian 2013 年 2 月 18 日
Actually, I solved this. For those who are concerned:
I switched my code in for he while loop with:
while ~isempty(find(bif_index == randomgen_connMx(:,2), 1)) == 1
upstreamFace = find(bif_index == randomgen_connMx(:,2));
propMx(upstreamFace,4) = propMx(upstreamFace,4)+1;
bif_index = randomgen_connMx(upstreamFace,1);
end
and got it to work instantly. I changed to check if the statement yielded a true statement by checking if the find argument yielded anything. Thats what I should have done earlier.

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by