Print out each matrix element
6 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!