MATLAB continue function skips the wrong interation - matlab

I have a "triple nested" for loop. I have a continue in my last for loop when it is equal to the first for loop (see code) I've tried to debug the issue in a separate function to see if I was missing something, but the for loop works fine in principle by itself. The issue is that when run, the continue function jumps out of the current for loop and into the second for loop. I've never seen a continue function do this, and I've tried about everything I can think of to make it right.
Here's the code:
for i = 1:n
acc = 0;
for c = 0:2
disp(i); **<---for loop continues to here instead of in the "j" for loop and going to j = 2**
disp(c);
for j = 1:n
disp(j);
if i == j
continue;
else
if c == 0
acc = acc + (m(j)/r(i,j))*(r0(3*j)-r0(3*i));
else
acc = acc + (m(j)/r(i,j))*(r0((3*(j-1))+c)-r0((3*(i-1))+c));
end
end
%multiplying by gravitational constant
acc = G*acc;
%defining velocity in terms of acceleration
if c == 0
dy(3*n+(3*i)) = acc;
else
dy(3*n+(3*(i-1)+c)) = acc;
end
disp(dy)
end
end
end
Here's the output:
n_body
#setup
1
0
1
1
1
1
Appreciate the help.

Related

Getting out of multiple for loops in MATLAB

I have a homework exercise that asks for a script that finds smallest 3-digit Armstrong Number.
I wrote my script. It works fine for finding these numbers, but I don’t know how to stop at first Armstrong number.
Here is my code
a=0;
b=0;
c=0;
for x1=1:1:9
a=x1^3;
for x2=0:1:9
b=x2^3;
for x3=0:1:9
c=x3^3;
d=(x1*100 + x2*10 + x3);
if (a+b+c) == (d)
disp(d)
end
end
end
end
It normally prints 153 370 371 407.
If I use break after disp it just gets out of first for and not all, and prints 153 370 407.
You can stop loops with the keyword break. However, this just stops one loop. You have now to options
set some kind of flag and check it in every loop to terminate (break) it (aka: the proper way)
use return (aka: the nasty way)
Option 1
a = 0;
b = 0;
c = 0;
FLAG_STOP = false;
for x1 = 1:9
a = x1^3;
for x2 = 0:9
b = x2^3;
for x3 = 0:9
c = x3^3;
d = (x1*100 + x2*10 + x3);
if (a+b+c) == (d)
disp(d)
% set flag
FLAG_STOP = true;
break
end
end
if FLAG_STOP
break
end
end
if FLAG_STOP
break
end
end
Option 2
a word of warning:
(...) it does not just exit the loop; it exits the script or function and
returns control to the invoking program or command prompt.
i.e. all statements after return will be ignored -- with no matter whether they are part of a loop or not
a = 0;
b = 0;
c = 0;
for x1 = 1:9
a = x1^3;
for x2 = 0:9
b = x2^3;
for x3 = 0:9
c = x3^3;
d = (x1*100 + x2*10 + x3);
if (a+b+c) == (d)
disp(d)
return % <<<<<<<<<<<<
end
end
end
end
If you don't want to use return as recommended in #max's answer you may use try/catch and error to handle such cases:
try
a = 0;
b = 0;
c = 0;
for x1 = 1:9
a = x1^3;
for x2 = 0:9
b = x2^3;
for x3 = 0:9
c = x3^3;
d = (x1*100 + x2*10 + x3);
if (a+b+c) == (d)
disp(d)
error("") ;
end
end
end
end
catch
end

MATLAB won't give out the answer

I wrote my own function in MATLAB which will returns me a "true" if the input number is a prime number and a "false" if it isn't.
With the numbers 0, 1, and 2 it's working, but with anything above 2 it's not doing anything. (BTW, I recreated isprime, so obv I won't use that here.)
function [A] = myprime(p)
m = 2;
if p<1
disp('Number too low')
end
if p == 1
A = false;
end
if p == 2
A=true;
end
if p < 2
while m < p
A = true;
x = mod(p, m);
if x == 0
m=p;
R=false;
end
m=m+1;
end
end
end
As you can see, it gives results for 0,1 and 2, but nothing for any number above 2:
there is a problem with your code, you don't declare the state where p>2 and also I think the line that you declared
if p<2
while m<p
is not true because you set m=2 and that state won't happen.

Matlab: Updating max count in a loop doesn't work

I have executed this simple loop script in MATLAB
clc;clear; close all;
m = 100;
for i = 1:m
if(i == 2)
m = 1000;
end
end
and found, that 'i' loops only till '100' BUT NOT '1000'. Why..??
The statement for i=1:m assigns the array 1:m to the list of values the operator will take on during the loop. This happens when the loop starts executing (note: you can use any array, and it'll be worked through column by column; for letter='abcde';fprintf('%s\n',letter);end works fine).
If you want to adjust how often your loop will be iterated through, I recommend using a while loop:
ct = 1;
maxIterations = 100;
success = false;
while ~success
fprintf('iteration %i/%i\n',ct,maxIterations);
ct = ct + 1;
if ct == 2
maxIterations == 1000;
end
if ct > maxIterations
success = true;
end
end
I'm not an expert but the for loop replace the m var with 100 in the first run and then it performs the loop as from 1 to 100 (not 1 to m) and it doesnt check every run what is m it knows that m is 100 and it runs until it reaches 100 if for example your script is like this:
<code>
m=100;
for i=1:m (m is 100)
if(i==2)
m=1000;
for i=1:m (m is 1000)
a=xyz;
end
end
end
</code>

while loop within while loop within an else

I have a while loop within an else statement. while the condition in the while loop is true I have the variable k decreasing (k=k-1). I want for the while loop to stop before k==0. I have tried placing the while loop within another while loop (while k>1) thinking maybe that would cancel the inner loop if k dropped to 1. Any thoughts?
if yada yada
do thing1
else
while k>1
while x==true
k=k-1;
do thing2
end
end
end
Doesn't this do the trick?
if yada yada
x = f(x) %// If you don't have anything here, you should use "if ~yada yada"
%// instead of "else"
else
while k>1 && x == true %// BTW: Lower case t in true
x = g(y) %// I hope you have something more than "k=k-1" in this loop
k = k-1;
end
end
You could add this sentence to be the last thing on your innermost while
if k <= 1
break;
Since your while-loop is basically achieving a counter-increment, I would highly recommend replacing it (actually, both of them) with a for-loop. In addition to being much more readable, it will allow Matlab to apply optimizations which will result in much faster execution times. See here:
yadayada = false;
x = true;
if yadayada
else
kmax = 5; % or whichever maximum value you want k to have
for k = kmax:-1:2 % have k start at kmax, decrement by 1 each time, until k==2
fprintf('k = %d\n', k);
% do something useful in the loop
if ~x
break;
end
end
end
Output:
k = 5
k = 4
k = 3
k = 2

Multiple while loop conditions

Here is my sample code, made to work out how to get a while loop to end when any one of three conditions are satisfied.
I want the code to end when n = 100, but it ends at n = 301. How can I get this to end at n=100?
clear all; close all;
n = 0;
R = 0; A = 0; T = 0;
while (R~=1) || (A~=1) || (T~=1)
if n == 100
R = 1;
end
if n == 200
A = 1;
end
if n == 300
T = 1;
end
n=n+1;
end
|| means or (with short circuiting). This means that your loop won't quit until all of the conditions are false.
You want to use AND, which is &&. This will mean the loop quits when at least one of the conditions is false.
ALSO (from the comments below):
Currently n will have a value of 101 when the loop finishes (because of the n=n+1 at the bottom of the loop). If it was important that the value of n was 100, then you could insert a break (info here) into the if body so that the loop quit when n = 100.