Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Help with a loop
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I will try to explain this best I can, as well as supply the code I have so far.
I have a large data set, split into separate variables by columns, each with a different label. So each variable is N x 1.
I am trying to create a loop that will say "if the value of some calculation (distance, in this case) between the nth value of X and the nth+1 value of X is less than 100, return the nth+1 value of Z. I have the x coordinate and y coordinate stored as separate values, so I'm just calling each of these, e.g. x(2) and y(2), comparing that distance to x(3) and y(3), and if the value is less than 100, I want it to return Z(3). I want the loop to return all values of Z when it is true that the distance between the nth value and the nth+1 value is less than 100.
E.g., I want the loop to start at row 2 and calculate the distance between row 2 and row 3 (using x,y coordinates), and if it's less than 100, return the value of row 3 from variable Z. Then compare row 2 to row 4, and if it's less than 100, return the value of row 4 from Z. And so on, until it is no longer true that the distance between 2 and the end of Y is less than 100. When it is not longer true, start the whole process over but start with row 3 and compare to row 4, then 3 to 5, and so on, this time returning all values of Z that hold for this condition in yet a new column.
So by the end I have a set of data that displays the values of Z for all the times row 2 was within 100 of any other row, all the times row 3 was within 100 of any other row, and so forth.
I have some code, but it only returns one value each time I run it, and I don't know why. Thank you.
n=2; %start at row 2
v=3; %start comparison with row 3
len = length(patient);
if n <= len
while sqrt((xCartCoor(n) - xCartCoor(v))^2 + (yCartCoor(n) - yCartCoor(v))^2) <= (1/10)%pythogrean distance
disp(lc(v)); %output variable
v = v+1; %go to next row
end
n = n+1; %move starting row to next row and repeat
v=2;
end
1 Kommentar
Antworten (1)
James Tursa
am 7 Feb. 2017
Bearbeitet: James Tursa
am 7 Feb. 2017
Note that your if-check only runs once, so the statements n=n+1 and v=2 near the end of that block have no effect. Maybe you meant this instead:
while n < len
And I think you need v = n + 1 instead of v = 2 near the end of that block.
Also, your inner while look has the potential to run off the end of the array with the v=v+1 statement since you do not check to see how large v is before you use it as an index. You should add a check for this.
Using for-loops may be more appropriate for your case than while loops with explicit increment code.
0 Kommentare
Diese Frage ist geschlossen.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!