How do I stop the calculation in a for loop?

Hello community!
First of all thanks for your help:)
n = 4;
v = rand(n,2);
for f = 1:1000
v = v-0.001
end
Thats already what I ve got. It gives me 1000 of matrixes back and alyways calculated by - 0.001. Now I ve got problem. If one element of the matrix (for example matrix Number 94) is already smaller than 0.001, then this element should be for all other matrixes ( 95 - 1000) equal to zero. I hope you have some advice! Thanks.

 Akzeptierte Antwort

The division by 50 is just to make the output shorter for this demonstration.
format long g
n = 4;
v = rand(n,2) / 50;
for f = 2:1000
temp = v - 0.001;
temp(temp < 0.001) = 0;
v = temp
if ~any(v(:)); break; end %all 0
end
v = 4×2
0.0117169155857542 0.00385373115144157 0.00322072350669969 0.0123980988902088 0.00540972017890342 0.00679499574461224 0.0119082492555772 0
v = 4×2
0.0107169155857542 0.00285373115144157 0.00222072350669969 0.0113980988902088 0.00440972017890342 0.00579499574461224 0.0109082492555772 0
v = 4×2
0.00971691558575421 0.00185373115144157 0.00122072350669969 0.0103980988902088 0.00340972017890342 0.00479499574461224 0.00990824925557724 0
v = 4×2
0.00871691558575421 0 0 0.00939809889020883 0.00240972017890342 0.00379499574461224 0.00890824925557724 0
v = 4×2
0.00771691558575421 0 0 0.00839809889020883 0.00140972017890342 0.00279499574461224 0.00790824925557724 0
v = 4×2
0.00671691558575421 0 0 0.00739809889020883 0 0.00179499574461224 0.00690824925557724 0
v = 4×2
0.00571691558575421 0 0 0.00639809889020883 0 0 0.00590824925557724 0
v = 4×2
0.00471691558575421 0 0 0.00539809889020883 0 0 0.00490824925557724 0
v = 4×2
0.00371691558575421 0 0 0.00439809889020883 0 0 0.00390824925557724 0
v = 4×2
0.00271691558575421 0 0 0.00339809889020883 0 0 0.00290824925557724 0
v = 4×2
0.00171691558575421 0 0 0.00239809889020883 0 0 0.00190824925557724 0
v = 4×2
0 0 0 0.00139809889020883 0 0 0 0
v = 4×2
0 0 0 0 0 0 0 0

3 Kommentare

Tank you very much Walter! Thats exactly what I was asking for!
Could you help me maybe one more time
That was your code. At the begiining I have a starting position (of spheres)
p=[r + (a-2*r)*rand(n,1),r + (b-2*r)*rand(n,1)
Then I will change the position and slow the spheres down. ( Thats why I was asking for the code)
v = rand(n,2);
for f = 1:1000
temp = v - 0.001 ;
temp(temp < 0.001) = 0 ;
v = temp ;
if ~any(v(:)); break;
end
The positon is always updating but unfortunately its not slowing down. It always uses the first matrix and not the other 999. Is it maybe possiible to index the matrix?
for f = 1:1000
p = p +v % is it possible to index v(f)?
end
p = [r + (a-2*r)*rand(n,1),r + (b-2*r)*rand(n,1)];
v = rand(n,2);
for f = 1:1000
temp = v - 0.001 ;
temp(temp < 0.001) = 0 ;
v = temp ;
if ~any(v(:)); break; end
p = p + v
end
gamer
gamer am 14 Jun. 2021
Thank you so much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 13 Jun. 2021

Kommentiert:

am 14 Jun. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by