Print out each matrix element

조회 수: 7 (최근 30일)
Ken L
Ken L 2021년 2월 4일
댓글: Ken L 2021년 2월 5일
Hi, I would like to ask, how can I print out all element in unknow matrix dimention?
E.g,
User will input any matrix dimension like [100 200 300; 400 500 600] or [100 200; 300 400; 500 600; 700 800] and etc. Then I will need to take all element do some formula and print like below,
prinnt out:
100 * formula = answer
200 * formula = answer
300 * formula = answer
400 * formula = answer
500 * formula = answer
600 * formula = answer
700 * formula = answer
800 * formula = answer
It depends on what matrix that user key in, I did tried use for index =1:length(user input) or size(user input), but both them seems cannot achieve what I want.
Thanks in advance.

채택된 답변

KSSV
KSSV 2021년 2월 4일
You need to read about fprintf.
formula = 3 ;
A = rand(3) ;
[m,n] = size(A) ;
for i = 1:m
for j = 1:n
ele = A(i,j) ;
val = ele*formula ;
fprintf('%f * %f = %f\n',ele,formula,val)
end
end
0.477980 * 3.000000 = 1.433940 0.560227 * 3.000000 = 1.680680 0.591175 * 3.000000 = 1.773526 0.376574 * 3.000000 = 1.129721 0.612415 * 3.000000 = 1.837244 0.914196 * 3.000000 = 2.742589 0.593840 * 3.000000 = 1.781520 0.123138 * 3.000000 = 0.369415 0.404010 * 3.000000 = 1.212029
  댓글 수: 1
Ken L
Ken L 2021년 2월 5일
Hi KSSV, thanks for help, its working.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by