a basic question for a beginner

조회 수: 4 (최근 30일)
Eliraz Nahum
Eliraz Nahum 2018년 9월 15일
편집: Stephen23 2018년 9월 17일
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.

채택된 답변

Stephen23
Stephen23 2018년 9월 15일
편집: Stephen23 2018년 9월 15일
"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
Stephen23
Stephen23 2018년 9월 17일
편집: Stephen23 2018년 9월 17일
Eliraz Nahum's falsely accepted "Answer" moved here:
Thanks!
Stephen23
Stephen23 2018년 9월 17일
편집: Stephen23 2018년 9월 17일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by