a basic question for a beginner

4 views (last 30 days)
Eliraz Nahum
Eliraz Nahum on 15 Sep 2018
Edited: Stephen23 on 17 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');
  1. 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).
  2. 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.

Accepted Answer

Stephen23
Stephen23 on 15 Sep 2018
Edited: Stephen23 on 15 Sep 2018
"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):
  4 Comments
Stephen23
Stephen23 on 17 Sep 2018
Edited: Stephen23 on 17 Sep 2018
Eliraz Nahum's falsely accepted "Answer" moved here:
Thanks!
Stephen23
Stephen23 on 17 Sep 2018
Edited: Stephen23 on 17 Sep 2018
@Eliraz Nahum: please accept my answer if it helped you. Adding your own answer saying "Thanks" and accepting that does not give me the credit for answering your question.

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!