how to create 3*5 matrix using loop?
9 次查看(过去 30 天)
显示 更早的评论
11) Create a 3 x 5 matrix. Perform each of the following using loops (with if statements if necessary): • Find the maximum value in each column. • Find the maximum value in each row. • Find the maximum value in the entire matrix.
2 个评论
dpb
2016-10-16
But I'll note the question doesn't match the HW question -- it doesn't say use a loop to create the array, only for the computation of the various maxima.
回答(2 个)
Austin
2016-10-16
Where did you become stuck?
The MATLAB Onramp explains creating matricides pretty well. Max function help is here: https://www.mathworks.com/help/matlab/ref/max.html
I thought the hardest part was figuring out what to loop. If you just start typing it in, the repeating commands are easier to identify.
Here is how to find the max of each row with a loop by indexing into each row of the created array:
a=rand(3,5)%There are a bunch of ways to put in better data; try MATLAB Onramp tutorial
b=zeros(1:3);%pre allocating for the row answer
for n=1:3
b(n)=max(a(n,:))%This returns the max of each row as an element of b.
end
0 个评论
Walter Roberson
2016-10-16
You can get the outline of the code from http://www.mathworks.com/matlabcentral/answers/307479-how-to-use-if-statement-to-pick-up-3-to-4-values-from-the-data-that-nearest-to-the-mean#answer_239173 which is code that finds the 4 smallest values of something.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!