# Matlab logical operators

• Mar 23rd 2009, 05:43 AM
Rorshach
Matlab logical operators
Hey,
I'm doing a model in matlab and for some reason the following logical statement will not produce what it is meant to produce.
The structure is the following:
initial values
for loop(j=1:1:10)
while loop(
while
G>e
i=i+1
...
AB(i)=j+...
G=abs(AB(i+1)-AB(i))
)
if staement (
if
(AB(i)<1)
AB
end
)
hope you got the point.
the trouble is that it prints all of the values of AB to the screen( for all the i's)-including AB=1.
what's wrong with it? I can't figure it out.
Any help would be most appreciated.
• Mar 23rd 2009, 06:43 AM
CaptainBlack
Quote:

Originally Posted by Rorshach
Hey,
I'm doing a model in matlab and for some reason the following logical statement will not produce what it is meant to produce.
The structure is the following:
initial values
for loop(j=1:1:10)
while loop(
while
G>e
i=i+1
...
AB(i)=j+...
G=abs(AB(i+1)-AB(i))
)
if staement (
if
(AB(i)<1)
AB
end
)
hope you got the point.
the trouble is that it prints all of the values of AB to the screen( for all the i's)-including AB=1.
what's wrong with it? I can't figure it out.
Any help would be most appreciated.

Well I don't recognise most of that as being in Matlab syntax.

CB
• Mar 23rd 2009, 07:08 AM
Rorshach
thanks,but:
I'ts just a discription of the code, I need some general advice about how to make it work and how the logical opertators work.
• Mar 24th 2009, 12:11 AM
math2009
This is a demo.
How to print elements by specified condition

AB = 2*rand(1, 10); % generating random elements
idx = []; % store index of selected elements

for i=1:10
if AB(i) > 1
idx = [idx i];
end;
end;

AB(idx) % display selected elements.