Hi.
i met a question on this program, this is professor's code.
While, i confused for the M(r,c) = input(': ');
What is purpose of this code and why it is used on this program ?
% question: let users enter 9 numbers and output a 3*3 matrix,
clear all
clc
rows = 3;
cols = 3;
M = [];
for r = 1:rows
for c = 1:cols
fprintf('Enter the value for row %d column %d',r,c);
M(r,c) = input(': ');
end
end

 채택된 답변

KSSV
KSSV 2022년 3월 31일

0 개 추천

You shoould run the code o understand it your self.
What is purpose of this code and why it is used on this program ?
The purpose of the line is to take the user input, for rth row and cth column. Read about the function input. Try
doc input
help input
There should be a small change in the code. Dont use
M = [] ;
You should initilaize the matrix M.
M = zeros(rows,cols) ;

댓글 수: 7

Shuoze Xu
Shuoze Xu 2022년 3월 31일
Ok, thank you.
Shuoze Xu
Shuoze Xu 2022년 3월 31일
While i know the meaning of input.
I just cannot understand why it used on here?
I know the meaning of fprintf() and input(), but why this code use fprintf() to show the comment for users
fprintf('Enter the value for row %d column %d',r,c);
M(r,c) = input(': ');
KSSV
KSSV 2022년 3월 31일
fprintf is used to print the information.
Shuoze Xu
Shuoze Xu 2022년 3월 31일
Yes, i known it.
My question is i always use the fprintf() to print infomation like that frprintf('The result is %d\n',vairable name)
But what is meaing of M(r,c) on this programming?
It means matrix?
KSSV
KSSV 2022년 3월 31일
Yes M is matrix and M(r,c) stands for element at rth row and cth column.
Shuoze Xu
Shuoze Xu 2022년 3월 31일
Sorry maybe MY description is not very clear, my main confusion is that the problem I encountered before is to write "add number" in input, I have never seen this kind of usage before.
i always use x = input("Enter the number: ")
fprint("The number is %d\n",x);
Shuoze Xu
Shuoze Xu 2022년 3월 31일
Anyway, thank you for your patience, thank you very much.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2021b

태그

질문:

2022년 3월 31일

댓글:

2022년 3월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by