Asked by Eliraz Nahum
on 15 Sep 2018

hello...I am starting learning Matlab by myself. there were some commands I could't understand and will be grateful if you answer me ASAP. I got the additional code (added as jpg file):

clc

close all

x=(1:0.1:10);

y=(x.*sin(x))./exp(x);

Ymin=min(y)

Xmin=x(y==Ymin)

plot (x,y);

hold on

plot (x,Ymin*(x==x),'r--');

plot (Xmin,Ymin,'*g');

- I have a problem to understand why Xmin=x(y==Ymin) makes the software to find the appropriate x value to the minimum Y value. According to what I learned the operation of == should be implemented on the same size subjecs, eg. scalar==scalar or vector==vector, what isn't the case here (vector==scalar). In addition the outcome of the above operation should be 0/1 - so I can't understand how this outcome represents the desired index (which is 32).
- I can't understand from the syntax why the command plot(x,Ymin*(x==x),'r--'); makes the software draw a horizontal line y=Ymax. I specially can't understand what the mid-term Ymin*(x==x) mean.

Answer by Stephen Cobeldick
on 15 Sep 2018

Edited by Stephen Cobeldick
on 15 Sep 2018

Accepted Answer

"According to what I learned the operation of == should be implemented on the same size subjecs, eg. scalar==scalar or vector==vector..."

Not according to the MATLAB documentation. In the eq help it states "Numeric or string inputs A and B must either be the same size or have sizes that are compatible (for example, A is an M-by-N matrix and B is a scalar or 1-by-N row vector). For more information, see Compatible Array Sizes for Basic Operations". When you follow the link in the help it takes you to a page that applies to many mathematical operations, and explains, together with examples, what (different) sizes the inputs arrays can have.

"I specially can't understand what the mid-term Ymin*(x==x) mean"

So try it yourself, take it apart and see what the different parts do. Essentially x==x produces an array the same size as x, but with all values 1 (or true), and then multiplies these ones with Ymin. For example:

>> x = [4,5,6,7];

>> x==x

ans = 1 1 1 1

>> 7*(x==x)

ans = 7 7 7 7

You could easily have tried that yourself at the command line. Experimenting is a great way to learn things!

Personally I would have made this a bit more explicit:

Ymin*ones(size(x))

Note that the "compatible array sizes" behavior changed significantly with MATLAB version R2016b, so you should always check the documentation for your installed MATLAB, not just the publicly accessible online help (which is for the most recent version):

Stephen Cobeldick
on 16 Sep 2018

" For the meanwhile can you please explain the below expression to me step by step? Xmin=x(y==Ymin) "

It returns the x values at the locations where the y values equal Ymin. y==Ymin creates a logical array. x(...) uses that logical array as a logical index to select the required values.

Stephen Cobeldick
on 17 Sep 2018

Thanks!

Stephen Cobeldick
on 17 Sep 2018

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Image Analyst (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/419144-a-basic-question-for-a-beginner#comment_610788

Sign in to comment.