Code for a function for matrixes?
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen

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 Kommentare
the cyclist
am 5 Mär. 2017
Bearbeitet: the cyclist
am 5 Mär. 2017
The first thing you need to do is forget about MATLAB for a moment, and just derive the algorithm for doing this, if you needed to do it by hand. What are the steps you would take? Be precise in defining those steps. (That is what an algorithm is.)
After you have done that, think about how to translate that into code. If you get stuck at that stage, post what you have tried, and you then maybe you are ready for some MATLAB help.
John Jamison
am 5 Mär. 2017
Bearbeitet: Image Analyst
am 6 Mär. 2017
Rena Berman
am 7 Mär. 2017
(Answers Dev) Restored edit
Antworten (1)
Image Analyst
am 6 Mär. 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 Kommentare
John Jamison
am 6 Mär. 2017
John Jamison
am 6 Mär. 2017
Steven Lord
am 6 Mär. 2017
Show the complete text of the messages you receive (all the red text, if this code throws an error, and/or the orange/yellow text, if it issues warnings.) Also show how you called the myMax function to generate those messages.
John Jamison
am 6 Mär. 2017
Image Analyst
am 6 Mär. 2017
Why did you decide to remove the critical line:
[rows, columns] = size(v);
?????
Usually when someone sees an error like that, they go up and see where the undefined variable was mentioned in the code. In your case you'd see it was no longer mentioned. Then you'd realize that it needs to be assigned to something before you can use it. See http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ for debugging tips.
Next, you don't want to return rows and columns - the size of the input array. What good is that? What they want is the maxes of the columns, and the rows where the maxes occur, both of which are computed in the function and available for return.
John Jamison
am 6 Mär. 2017
Image Analyst
am 6 Mär. 2017
How is it not working? I can't see how you called it. Did you do
[columnMaxes, rowsOfMax] = myMax(v)
??? Remember the automated tester expects it to be called myMax, not myFunc as you called it.
John Jamison
am 6 Mär. 2017
Walter Roberson
am 6 Mär. 2017
You have to initialize the variable to something. You want to initialize it to something that every possible numeric value will be greater, so that you do not accidentally get the initialized value instead of the true maximum value. For example if you initialized to -100 but it turned out there was a value -255 in the data, then max(-255, -100) would be the -100 you initialized to, but that is not what you want -- you do not want that initialization value (-100 in this example) interfering with the data. -inf has the right properties: every numeric value is greater than -inf (except for -inf itself).
John Jamison
am 6 Mär. 2017
Walter Roberson
am 6 Mär. 2017
It should be something smaller than any valid value. If you know for sure that you will never have negative values, then Yes it could have -1
Watch out, though, for what you report as being the maximum if the matrix turns out to be empty.
Image Analyst
am 7 Mär. 2017
Is it the inf line that you say is not working? I just tried it and it works fine:
>> columnMaxes = -inf * ones(1, columns)
columnMaxes =
-Inf -Inf -Inf -Inf -Inf
I tested the code before I posted it and everything worked fine. SO I can't know what's not working for you unless you show us how you called it. In your screenshot, you chopped off the left hand side and it looks like one one array is being printed so I don't think you accepted both return values. Please show the entire line, and please tell us exactly what "not working" means, because I don't know because mine works perfectly.
John Jamison
am 7 Mär. 2017
Image Analyst
am 7 Mär. 2017
Like Walter said, when you're searching for a maximum you need to find a value bigger than the maximum so far. The only way to make sure that any number whatsoever will be chosen is to make the max minus infinity. This is typical standard computer science. Actually you could make the value the first row of the array and that would work too.
I used zeros just to preallocate space to speed it up. For small arrays like this it's not needed - you could omit it. But for large arrays (like a million elements) it speeds things up so it's just good practice.
John Jamison
am 7 Mär. 2017
Bearbeitet: John Jamison
am 8 Mär. 2017
wait, @John Jamison please help. someone deleted their question!
When you edit your question text away then you are basically treating us like your own personal MATLAB consultants, and by deleting your question you unilaterally decide if our answers should make sense to anyone else. But we volunteer to help on this public forum to help everyone who reads this forum (and this thread), not just you.
If you want personal, private MATLAB consulting and not to help anyone else who reads this forum, then you can find such services here:
or at any of the many consulting companies who will be happy to help you in return for a few pesetas.
John Jamison
am 8 Mär. 2017
John Jamison
am 8 Mär. 2017
Walter Roberson
am 8 Mär. 2017
Bearbeitet: Walter Roberson
am 8 Mär. 2017
As your first line inside the function,
if isvector(v); v = v(:); end
Diese Frage ist geschlossen.
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

