How can I make it so a while loop doesn't stop if one of the elements in a vector going into it fulfills the condition?
23 views (last 30 days)
Show older comments
My code looks something like this:
while(sqrt(x.^2 + y.^2 + z.^2)<1)
some conditions which increases the values of x, y, and z
end
I want to be able to input vectors for x, y, and z, but if I try to the while loop breaks immediately since one set of x, y, and z reaches the condition before the other sets in the vector does. I want to make it so that the while loop continues to work on those elements that have not yet fulfilled the condition while stopping to work on those that already have.
Say for example I set that:
x = [0.5 0.2 1.2];
y = [0.5 0.3 1.0];
z = [0.6 0.1 0.9];
The third set of x, y, and z would then already fulfill the condition and the code stop doing the other conditions for the first two sets of values. Is there any way to make it work?
Thanks so much for any help!
0 Comments
Answers (1)
Rajanya
about 20 hours ago
As correctly pointed by you, the loop stops because the condition in the while loop is in vectorized form and whenever any one set of values for ‘x’,’y’ and ‘z’ surpasses the condition, it breaks.
To prevent this, you can take the help of logical indexing to frame the loop condition, ensuring it only includes those indices of the ‘x’,’y’ and ‘z’ vectors for which the ‘sqrt(x.^2 + y.^2 + z.^2)’ is still less than 1. This way, all further computations can be made to only those indices.
Here’s a sample code to demonstrate the following:
res = sqrt(x.^2 + y.^2 + z.^2);
on = find(res<1);
while(sqrt(x(on).^2+y(on).^2+z(on).^2)<1) % check only for the indices under consideration
x(on) = x(on)+0.1;
y(on) = y(on)+0.1; %computations for the indices
z(on) = z(on)+0.1; %under consideration
res = sqrt(x.^2 + y.^2 + z.^2);
on = find(res<1); %see which new indices satisfy the condition
end
Following this, with the provided ‘x’,’y’ and ‘z’ vectors, the loop only terminates when all sets of values for ‘x’,’y’ and ‘z’ fails the loop condition, as confirmed by the variable 'res' after the termination of the loop.
To learn more about vectorization and how to handle it using logical indexing, you can refer the documentation by visiting the documentation page after executing the following command from the MATLAB Command Window:
doc vectorization
Hope this helps!
2 Comments
Stephen23
about 20 hours ago
"To prevent this, you can take the help of logical indexing to frame the loop condition..."
Good idea, but the code uses FIND() which means that this answer does not actually use logical indexing:
Here is much the same approach, this time actually using logical indexing:
x = [0.5,0.2,1.2];
y = [0.5,0.3,1.0];
z = [0.6,0.1,0.9];
res = sqrt(x.^2 + y.^2 + z.^2);
idx = res<1;
while any(idx)
x(idx) = x(idx)+0.1;
y(idx) = y(idx)+0.1;
z(idx) = z(idx)+0.1;
res = sqrt(x.^2 + y.^2 + z.^2);
idx = res<1;
end
res
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!