Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Code for a function for matrixes?

1 visualización (últimos 30 días)
John Jamison
John Jamison el 5 de Mzo. de 2017
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
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 comentarios
John Jamison
John Jamison el 5 de Mzo. de 2017
Editada: Image Analyst el 6 de Mzo. de 2017
function [a b] = myMax(v)
maxHigh = 0;
currHigh = 1;
for i = 1:length(v)
if maxHigh > currHigh
currHigh = maxHigh
end
end
Rena Berman
Rena Berman el 7 de Mzo. de 2017
(Answers Dev) Restored edit

Respuestas (1)

Image Analyst
Image Analyst el 6 de Mzo. de 2017
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 comentarios
John Jamison
John Jamison el 8 de Mzo. de 2017
@Image Analyst
Walter Roberson
Walter Roberson el 8 de Mzo. de 2017
Editada: Walter Roberson el 8 de Mzo. de 2017
As your first line inside the function,
if isvector(v); v = v(:); end

La pregunta está cerrada.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by