Help understanding matlab code ?

조회 수: 7 (최근 30일)
Modus Operandi
Modus Operandi 2014년 9월 6일
댓글: Image Analyst 2016년 10월 6일
I am completely new to Matlab and I also don't have Matlab - but I have been asked to convert some Matlab code to another language, which is proving a little bit tricky for me. At the moment I am stuck with the following lines which are within a loop:
d=s2(1)*ones(dim(1),1);
for i=2:R,
d=[d;s2(i)*ones(dim(i),1)];
s2 is a 1x2 matrix [0.1 1], dim = [3], and R is equal to 1 (the first time this code is evaluated)
My understanding is that the first line results in the 3x1 matrix
0.1
0.1
0.1
But I am confused by what happens in the for loop a) since R is 1, is this a decrementing loop (evaluated with i=2 then i=1 ?) b) I really don't get the line d=[d;s2(i)*ones(dim(i),1)] at all
Could anyone help explain this to me ?

채택된 답변

Image Analyst
Image Analyst 2014년 9월 6일
편집: Image Analyst 2014년 9월 6일
No. First of all dim(1) doesn't work - throws error. But I'll assume you copied it incorrectly and that it is a 3 by 1 column vector. What the code does is to tack on additional columns with different values that depend on s2's value. For example if s2(2) = 12.34 then the second column of d will be [12.34;12.34;12.34]. Then the third column of d will have elements with the value of s2(3), and so on. If R equals 1, then it doesn't even enter the loop at all so d stays the same as before the loop.
The loop does not decrement from 2 to R - it increments. To decrement, there would have to be a step of -1, like
for i = R : -1 : 2
  댓글 수: 3
Image Analyst
Image Analyst 2014년 9월 6일
If you defined dim that way, then yes, dim(1) will be 3. Also, since R=1 the loop never gets entered, as I said earlier, and so basically the loop does nothing and can be deleted, unless you might ever have situations where R does not equal anything less than 2.
Modus Operandi
Modus Operandi 2014년 9월 6일
Ahh - OK - that makes sense - thank you !

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

추가 답변 (2개)

dpb
dpb 2014년 9월 6일
for i=2:R
for R=1 is an empty loop
Matlab for loop control is
for start:[increment:]end
with the default for increment when not given of +1. If initial end < start, the loop body is never entered. The code as written is the same as
for i=2:1:R
and a decrementing loop would require
for i=2:-1:R
the step variable cannot be skipped in that case. It's same as Fortran DO with the optional increment excepting Fortran writes the increment last instead of in the middle. The difference is that Matlab syntax follows that for vectors with the colon operator for consistency.
I really don't get the line d=[d;s2(i)*ones(dim(i),1)]
If R>=2, then it concatenates the results to the initial d to build a longer column vector of the elements of s repeated the number of times in the dim array.

Harmon Harmon
Harmon Harmon 2016년 10월 5일
Hi, I'm trying my best to understand this code and how everything works together. I know that there are some comments, which are in green and I understand that.
  댓글 수: 1
Image Analyst
Image Analyst 2016년 10월 6일
How is this an answer of Modus's original question?
Do you have a question? Or is this just an announcement?

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by