Ignore Condition on 1st Loop - matlab

if(Z1(m)<Z1(m-1)
IN=IN+1;
M1(:,IN)=Vb(:,t);
else
FCAST=Vb(N,T);
break;
end
I have this condition on my program, but the problem is it cannot work for first iteration because Z1(m)
Did anyone know how to solve this?

You can put the code you would like in the first iteration in it's own if statement
IF in == 1 || Z1(m)<Z1(m-1)
...
That might work.

Related

How to return boolean in if else statement and the return value will link to anothe if else statement in Matlab?

I have the following if else statement that created by myself in order to link to the if else statement given in second part:
m=4
if m==3
disp(true)
else
disp(false)
Second part ( this code is fix cannot be change):
if (true)
A=Hello World
else
A=Bye
If using the first part code, my output will be
A=Hello World
but my desire output is
A=Bye
Anyone one have idea to edit the first part, because now my return value in first part not able to link to my second part.
If you can't change the second part's code, I'm afraid your desire cannot be fulfilled. Or rather, I'm afraid your code won't run at all, because your perenthesis, quotes, end-statement (and arguably semicolons) are not in place.
if true
A = 'Hello World';
else
A = 'Bye';
end
This code will return A = 'Hello World', no matter what, since true is always true. If-else conditions work like this:
if (*what's in here evealuates to true*)
%do stuff
else (*if what's up there does not evaluate to true*)
%do other stuff
Clearly, true will always evaluate to true. So the above if-else condition will always return A = 'Hello World'.
You don't need two if statements in order to accomplish this task. One is more than enough to perform all what you need:
m = 4;
if (m == 3)
A = 'Hello World';
else
A = 'Bye';
end
disp(A);
A few comments concerning your code:
if statements need to be closed with an end
if (true) will always pass into the statement
the disp function doesn't assign a value, its only goal is to display it in the Command Window
in order to work with text, you have to enclose it within single quotes ' (char array) or double quotes " (string), more info here
If you posted only small excerpts of your code and you need to perform those two checks sequentially, in different parts of your script, then:
m = 4;
if (m == 3)
m_equals_3 = true;
disp('M == 3');
else
m_equals_3 = false;
disp('M ~= 3');
end
% then, somewhere else...
if (m_equals_3)
A = 'Hello World';
else
A = 'Bye';
end
% ...
I guess this is a homework exercise. You should disclose that if it’s the case.
The exercise requieres you to change the workspace such that the second bit of code evaluated the else case. This can be accomplished by changing the meaning of true. In your first bit of code, make it so that
true = flase;
Or equivalently,
true = 0;
Note that this is really bad form, if you ever do something like this outside of a homework exercise that explicitly asks you to do so, you’ll get fired or maybe even shot. You’ve been warned!
By the way, I assume that the missing quote characters around the strings and the missing ends are typos?

Is there any alternative for goto-statements in MATLAB?

I have a problem with writing a program in MATLAB. I need a goto-statement for solving my problem. Here is the code:
for i=1:n
1: newT=T(i)+unifrnd(-r,r);
newT=P(i)+unifrnd(-r,r);
if newT<Tmax && newT>Tmin && newP<Pmax && newP>Pmax
bestT=newT bestP=newP
else
go to 1
end
end
Is there any alternative for that goto-statement?
Is something like this what you're looking for?
for i=1:n
loop = true;
while loop
newT=T(i)+unifrnd(-r,r);
newT=P(i)+unifrnd(-r,r);
if newT<Tmax && newT>Tmin && newP<Pmax && newP>Pmax
bestT=newT bestP=newP
loop = false;
end
end
end
You can use this submission at MathWorks ® : https://www.mathworks.com/matlabcentral/fileexchange/26949-matlab-goto-statement
However, I recommend you to modify your code instead of using that!
you can try switch-case method. Please see example below;
caseno = input('input your case no');
switch (caseno)
case 1
disp('this first section will run');
case 2
disp('this second section will run');
otherwise
disp('wrong case no');
end

How to move the control to the else part of "if-else" when error comes in the statements under 'if' part?

Is there a way to make condition such that if error comes in the statements/matrices under if then execute statements under else?
i.e .
if (some condition)
some statment1/matrix1/variable1
some statement2/matrix2/variable2
some statement3/matrix3/variable3
else newstatement/matrix %come to else part of the code if any of the statements 1,2 or 3 under if condition yields any error like dimension mismatch or anyother
end
No built-in mechanism to jump to else section. But you can use a construct like this:
condition_flag = (some condition);
error_flag = false;
if condition_flag
try
some statment1/matrix1/variable1
some statement2/matrix2/variable2
some statement3/matrix3/variable3
catch
error_flag = true;
end
end
if ~condition_flag || error_flag
%if any of the statements 1,2 or 3 under if condition yields any error like dimension mismatch or anyother
....
error_flag = false;
end
Alternative solution to the one of Mohsen Nostrania.
else_flag=true;
if (condition)
try
%% The if code
else_flag=false;
end
end
if else_flag
%% The else code
end
The else_flag=false; command is executed only when condition is true and no error occured within the <if code>.
Be aware that both solutions do literally what you want - the <if code> commands are executed until an error occur and then the <else code> is executed.
If one want to reduce tre number of if statements:
try
if statement
%% The if code
else
%% Obvious error
a(0);
end
catch
%% The else code
end

Variable input to MATLAB function resets involuntarily inside while loop. How can I prevent it?

I have written a function which takes in an integer (int8) as one of the inputs (called iscool). The function runs a while loop and I insert an if-check inside it to break out of the loop. The if-check checks the value of the iscool variable as well and sets the value of imarch to 0 to get out of loop. So basically, the code is something like this.
% Code_snippet
while (imarch == 1)
<some procedures not modifying iscool>
if ((iscool == 0) && (<other condition 1>) && (<other condition 2>))
imarch = 0;
elseif ((iscool == 1) && (<other condition 3>) && (<other condition 4>))
imarch = 0;
end
disp (strcat('Cooling index is: ',num2str(iscool)));
end
The output of the disp command in the first while-loop execution is 0 (which is the input), but it changes to 1 in the subsequent iteration and stays so after that. I have tried removing the if-elseif-end check and the value of iscool stays intact in that case, but I need to have the check in order to be able to get out of the loop. Any sort of help, particularly an insight into why the value might be changing would be great help. Thanks.

use debugger in matlab

i want to debug following simplest code in matlab and clarify why it executes always if statement
function testfile(x)
if 3<x<6
disp('in the middle of range');
else
disp('out of range');
end
end
i have used following code for debugger
echo testfile on
testfile(-2)
in the middle of range
testfile(6)
in the middle of range
why it does not execute else statement?i have used following code as a test
5<4<8
ans =
1
so does it mean that writing if statement in this style is wrong?a i understood it is same as if 5<4 || 4<8?then it makes clear for me why it executes only if statement and never reaches to else
5<4<8 is evaluated as (5<4)<8. If we resolve the expression in the parentheses first, we have 0<8, which is true. Test with 5<4==0, which evaluates to true.
What you want to do is check whether x is both bigger than 3 and smaller than 6, i.e.
3<x && x<6