Finding maximum of rows, colums, overall in matrix
5 次查看(过去 30 天)
显示 更早的评论
I am completely stumped on a problem here. I've been playing around with it for hours now to no avail. I have to create a script that will prompt the user to enter their desired matrix dimensions. Then perform each of the following using loops (Not using that ‘max’ built in function’ but you may use 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.
回答(1 个)
Image Analyst
2013-10-30
Hours? Wow. Hint:
[rows, columns] = size(yourMatrix);
maxOfRows = -inf * ones(1, rows);
maxOfColumns - .....
for col = 1 : columns
for row = 1 : rows
if .... > ...
end
end
end
I hope that's enough to get you started. See if you can fill it out from there.
3 个评论
Image Analyst
2013-10-31
You want to see if your matrix is greater than the max for the row and column the current element is in, so the first part is right, but you don't want to compare it to zero, you want to compare it to the latest max value for that row and column, don't you? And if it is, you need to store that value into the max arrays. And to check the current location, you check mat(row, col) because row and col are the indexes which change as you move through the array. rows and columns don't change so mat(rows, columns) refers to the very last (lower right) corner of the matrix and never changes.
if mat(row, col) > maxOfRows(row)
% Save this value in our max matrix.
maxOfRows(row) = mat(row, col);
end
if mat(row, col) > maxOfCols(col)
maxOfCols(col).........
Try to finish that.
Cedric
2017-9-22
Catherine, start a new thread, reference this one if necessary, and give an example. The more specific you are, the more likely you'll get an answer.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!