Info

此问题已关闭。 请重新打开它进行编辑或回答。

Code for a function for matrixes?

1 次查看(过去 30 天)
John Jamison
John Jamison 2017-3-5
关闭: MATLAB Answer Bot 2021-8-20
Hi,
Can I have some help for this code for the manual entering of a max function?
I was thinking of doing a if statement to keep tracking each number, but I am stuck. I currently have:
function [a b] = myMax(v)
maxHigh = 0;
currHigh = 1;
for i = 1:length(v)
if maxHigh > currHigh
currHigh = maxHigh
end
end
Thank you
  3 个评论
John Jamison
John Jamison 2017-3-5
编辑:Image Analyst 2017-3-6
function [a b] = myMax(v)
maxHigh = 0;
currHigh = 1;
for i = 1:length(v)
if maxHigh > currHigh
currHigh = maxHigh
end
end

回答(1 个)

Image Analyst
Image Analyst 2017-3-6
John, your code is very close but you need to initialize the variables and store the row the max shows up in. I renamed some variables and saved the row. Here's a little more to get you going:
function [columnMaxes, rowsOfMax] = myMax(v)
[rows, columns] = size(v);
columnMaxes = -inf * ones(1, columns);
rowsOfMax = zeros(1, columns);
for col = 1 : columns
for row = 1 : rows
thisValue = v(row, col);
if thisValue > columnMaxes(col)
columnMaxes(col) = thisValue;
rowsOfMax(col) = row;
end
end
end
end
See that it works for both 1-D arrays (both column vectors and row vectors), and 2-D arrays. You might put in a check to see if it's a 3-D array and popup an error message with sprintf(), uiwait(), and errordlg().
  19 个评论
Walter Roberson
Walter Roberson 2017-3-8
编辑:Walter Roberson 2017-3-8
As your first line inside the function,
if isvector(v); v = v(:); end

此问题已关闭。

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by