using IF and && together

165 views (last 30 days)
ZM
ZM on 4 Feb 2018
Commented: dpb on 7 Feb 2018
hello I have this prog that I combine if and & in my first prog in the line of the if the expression before the && is correct but the expression after the && is wrong it should give me an error when j is equal to 6 at that time it will read(x(j+1) =x(7) and I d not have x(7) but it does not. however if I write in the line of the if, this statement
[ if x_new(i)<x(j+1)&&x_new(i)>x(j)]
it gives the error
why when I write x_new(i)<x(j+1) after the && it does not give me an error? ===========================================================================
clc
clear all
x=0:0.2:1;
y=[0 0.199 0.389 0.565 0.717 0.84];
x_new=[0.1 0.33];
for i=1:length(x_new)
for j=1:length(x)
if x_new(i)>x(j) && x_new(i)<x(j+1)
y_new(i)=(((y(j+1)-y(j))/(x(j+1)-x(j)))*(x_new(i)-x(j)))+y(j)
end
end
end
your help is appreciated thank you Zhina

Accepted Answer

dpb
dpb on 4 Feb 2018
The "why" is because the && operator "short-circuits"; IOW, if the first condition is met so the answer is known from the first condition irrespective of the latter then the latter isn't ever computed as there's no need for it.
If you write it in the other order, then MATLAB will evaluate the expression in the order it is written and so try to make the test and discover an "out-of-range" error when you try to reference x(j+1) where j=length(x).
To fix this, don't try to access elements outside the array; fix the for...end loop indices such that only reference elements in range in case the first test isn't satisfied. Of course, then you'll need some other error-checking as you'll have given the algorithm data that is outside the range.
for j=1:length(x)-1 % keep references to x(j+1) in bounds; limit upper loop limit
  12 Comments
ZM
ZM on 6 Feb 2018
Thank you for your answers. I really appreciate it. about trying on an older version, I use both 2013 and 2017. but the result was the same. Thank you again.
dpb
dpb on 7 Feb 2018
My question had to do with whether TMW changed this behavior with "classic" operators when they introduced the short-circuiting versions which, as noted, isn't documented just when but relatively recently although a number of releases ago by now...I'm guessing was after R14 but I haven't yet reinstalled earlier versions on new machine since old one crashed and burned. Unless some of those old clients call don't know there will be any need and even then probably just data and m-files will be all will really need.

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 4 Feb 2018
The simplest way to do what you want is to not bother at all with loops at all and use interp1
x=0:0.2:1;
y=[0 0.199 0.389 0.565 0.717 0.84];
x_new=[0.1 0.33];
y_new = interp1(x, y, x_new);
If you really wanted to do it yourself then you would not use a loop to scan x:
x=0:0.2:1;
y=[0 0.199 0.389 0.565 0.717 0.84];
x_new=[0.1 0.33];
y_new = zeros(size(x_new)); %always preallocate your output
for i = 1:numel(x_new)
j = find(x_new(i) > x(1:end-1) & x_new(i) < x(2:end)); %can't use && because working on vector
y_new(i) = (((y(j+1)-y(j))/(x(j+1)-x(j)))*(x_new(i)-x(j)))+y(j)
end
Note that I've left the comparisons as you wrote them. They would produce an incorrect result if x_new(i) is exactly equal to any of the x values.

Community Treasure Hunt

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

Start Hunting!