MATLAB Answers

understanding "<" operator line

30 views (last 30 days)
fima v
fima v on 10 Apr 2021 at 15:59
Edited: per isakson on 10 Apr 2021 at 17:47
Hello,I cant understand a line of the line bellow taken from the code bellow.
I think i generates a circle but i cant see how .
When i tried to use "<" operator it ony gave me true or false
Could you please give me simple example of using this principle of the line bellow?
midCirc= X.^2+Y.^2<(1/n)^2;
n=1024;% matix dimmensions
lambda=633e-9;% wave length in [m]
L=0.01;% meter factor, it is defined that 1cm=1024 pixels-> the first scale will be over 1cm
scale=linspace(-L/2,L/2,n);% basic scaling vector in length of 1024.
%% Q2 creating matrix
[X, Y]= meshgrid(scale,scale);% creating the basic function. on the function we will create the images that will operated
midCirc= X.^2+Y.^2<(1/n)^2;%because we work in meter units, the 100/1024[cm] transforms into 1/1024[m]
Ring= ((X-0.8/n).^2+(Y-0.8/n).^2<(1/n)^2)&((X-0.8/n).^2+(Y-0.8/n).^2>(0.6/n)^2);
Tshape= (abs(Y)<0.2/n)&(abs(X)<0.6/n) | ((Y<1.4/n)&(Y>0.2/n)&(abs(X)<0.2/n));
colormap gray;

Accepted Answer

John D'Errico
John D'Errico on 10 Apr 2021 at 16:45
In order to understand this, I'd STRONGLY recommend you do the MATLAB Onramp tutorials. Try some examples. Look at what happens! For example:
A = magic(5)
A = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
What happens if we try this?
A < 10
ans = 5×5 logical array
0 0 1 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 1 1
So we see a logical array a boolean thing, true where the corresponding element of A was less than 10, false where that condition was not true.
And that is no different from what was constructed in the code you show. TRY IT. LOOK AT THE RESULT.

More Answers (1)

per isakson
per isakson on 10 Apr 2021 at 16:29
Edited: per isakson on 10 Apr 2021 at 17:33
Consider the two lines
midCirc = X.^2+Y.^2 < (1/n)^2;
NmidCirc = not(midCirc);
The first line assigns a logical value (true/false) to midCirc. (As you already noticed.) The LHS of the following line expects a logical value. The input argument of the function, not(), must (IMO) be logical. (Whoever created this code intended a logical value.)
It's possible to plot logicals
>> imagesc(Tshape)
per isakson
per isakson on 10 Apr 2021 at 17:45
@fima v, I just run the script of your question and the statement, imagesc(Tshape). No other code is involved.
In the same way, your script and
>> imagesc(NTshape)
>> colormap gray;

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by