필터 지우기
필터 지우기

anyone can help me explain the meaning of the following code?

조회 수: 1 (최근 30일)
Shwe
Shwe 2024년 5월 6일
댓글: Shwe 2024년 5월 6일
data = [ 1 100 500; 2 200 600; 3 300 700; 4 400 800 ];
headers = {'ID', 'Population', 'Revenue'};
for i = 1:size(data, 1)
fprintf('%-4d %-12d %-10d\n', data(i, :));
end
1 100 500 2 200 600 3 300 700 4 400 800
  댓글 수: 4
Torsten
Torsten 2024년 5월 6일
편집: Torsten 2024년 5월 6일
It defines a loop over i where i runs from 1 to the number of rows of the matrix "data" (the "1" in size(data,1) refers to the first dimension of the matrix "data", thus the number of rows).
Since "data" has 4 rows, the loop runs from 1 to 4.
Shwe
Shwe 2024년 5월 6일
'It defines a loop over i where i runs from 1 to the number of rows of the matrix "data" (the "1" in size(data,1) refers to the first dimension of the matrix "data", thus the number of rows).
Since "data" has 4 rows, the loop runs from 1 to 4.'
Dear Torsten,
Thank you for your explaination.
regards,

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

채택된 답변

the cyclist
the cyclist 2024년 5월 6일
편집: the cyclist 2024년 5월 6일
I ran your code here. It seems pretty obvious what it does, right?
You might want to do the free online MATLAB Onramp tutorial, if you don't know what a for loop does. You could also read the documentation page for fprintf.
Perhaps the only tricky part is the exact explanation of the formatting strings such as
'%-4d'
That is explained in that documentation page, in the formatSpec section.

추가 답변 (0개)

카테고리

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