what am i getting a syntax error when i run this code?
Show older comments
function [mmr,mmm] =minimax (M)
mmr = abs (max(M(1:end)- min(M(1:end))))
mmm = max (M:)-min(M:)
1 Comment
Marcel
on 8 Nov 2022
When changing the code to this i get the following output
function [mmr,mmm] =minimax (M)
mmr = abs (max(M(1:end)- min(M(1:end))))
mmm = max (M(:))-min(M(:))
Not enough input arguments.
Error in test (line 3)
mmr = abs (max(M(1:end)- min(M(1:end))))
Answers (1)
Okay so after using this code
function [mmr,mmm] = minimax (M)
mmr = abs( max(M(:)) - min(M(:)) );
mmm = max(M(:)) - min(M(:));
end
and calling it in the live editor as followed
>> test 10
i receive this following output
>> test 10
mmr =
1
mmm =
1
ans =
1
I dont know what this code is supposed to do but its not throwing any error anymore
5 Comments
Rik
on 8 Nov 2022
This is a common homework assignment. You can find the full assignment text in this thread (and probably several others):
"Write a function called minimax that takes M, a matrix input argument and returns mmr, a row vector containing the absolute values of the difference between the maximum and minimum valued elements in each row. As a second output argument called mmm, it provides the difference between the maximum and minimum element in the entire matrix."
You didn't actually explain why your code runs and the code in the question didn't.
The reason is that M: is not a valid Matlab syntax. I would have suggested the edit below.
function [mmr,mmm] = minimax (M)
mmr = abs( max(M(:)) - min(M(:)) );
mmm = max(M(:)) - min(M(:));
end
But this will not provide the correct answers.
Marcel
on 8 Nov 2022
It runs because magic. Like i said dont really know what its doing just tried to get rid of syntax errors. My code is like magic. Sometimes i get a error and dont know why. sometimes it works and i dont know why. Same as when it works.
Rik
on 8 Nov 2022
It sounds like you would really benefit from a basic Matlab tutorial. You may consider doing the Onramp tutorial (which is provided for free by Mathworks).
and calling it in the live editor as followed
>> test 10
i receive this following output ... I dont know what this code is supposed to do
That line of code is equivalent to passing '10' (a char vector) into your test function not 10 (the number) into it. In ASCII characters '0' and '1' are adjacent, so '1' - '0' is 1. [This makes a nice way to convert the text representation of a number into a vector containing its digits.]
one = '1' - '0'
x = '0123456789' - '0'
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!