필터 지우기
필터 지우기

How to write FOR LOOP in Matlab?

조회 수: 16 (최근 30일)
nancy
nancy 2014년 11월 25일
답변: Meghana Dinesh 2014년 11월 25일
Hi;
Could you please explain below code? I study on for loop in Matlab to learn. I use generally tutorials from the Internet. I have seen below code in the Internet. But I could not understand what that code means.
A = [ [1 2 3]' [3 2 1]' [2 1 3]']
A =
1 3 2
2 2 1
3 1 3
for j=2:3,
A(j,:) = A(j,:) - A(j-1,:)
end
For example; What does "for j=2:3," row means?
What does "A(j,:) = A(j,:) - A(j-1,:)" means? I couldn't understand that as well.. Could you help me?
  댓글 수: 1
the cyclist
the cyclist 2014년 11월 25일
You might want to start with some very basic tutorials. For example, take a look at this page.

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

답변 (2개)

the cyclist
the cyclist 2014년 11월 25일
The code
for j=2:3,
A(j,:) = A(j,:) - A(j-1,:)
end
means that you will run the middle line of code substituting the value j=2, and then you will run it substituting the value j=3.
So, it is the same thing as running the two lines of code
A(2,:) = A(2,:) - A(2-1,:)
A(3,:) = A(3,:) - A(3-1,:)
which is the same as
A(2,:) = A(2,:) - A(1,:)
A(3,:) = A(3,:) - A(2,:)
The colons in the second argument mean "do this for the whole row". It is a vector operation.

Meghana Dinesh
Meghana Dinesh 2014년 11월 25일
[1 2 3]' means transpose of [1 2 3] so it becomes:
[1
2
3]
This is the first column of A Similarly, you get [3 2 1]' and [2 1 3]' to get the second and third columns of matrix
for j=2:3,
means "for j equal to 2, till 3, in steps of 1 (by default, it is in steps of 1). So j takes the values 2 and 3.
A(j,:)
Here, j takes the values 2 and 3. The syntax is: A(1st dimension,2nd dimension). The 1st dimension is row, 2nd dimension is column. So the row of A takes the value 2 in the first iteration and 3 in the second iteration.
Example: A(2,:) means 2nd row of A and all columns. So, in the A matrix defined above, you will get
A(2,:) = [2 2 1]

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by