I see you did answer the question in my comment.
"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"
This seems a little confusing as a question, because the difference between the max and min will ALWAYS be a positive number. So I'm not at all sure why it seemed important to indicate the absolute value. I guess that just means those writing homework assignments are human too?
Anyway, the global max minus the min is easy to write, as is the row-wise result.
function [mmr,mmm] = minimaxTrial(M)
mmm = max(A,[],'all') - min(A,[],'all');
mmr = max(A,[],2) - min(A,[],2);
end
One thing you don't want to do, is to extract each row of the matrix as you did.
C_1 = (A(1,1:col));
C_2 = (A(2,1:col));
C_3 = (A(3,1:col));
C_4 = (A(n,1:col));
That never works, because what happens if tomorrow your matrix has 3 or 5 rows? That little n at the end does not tell MATLAB to extend the pattern for n rows. (I suppose it should, but computers are not that smart. Well, not yet.)
Instead, you either needed to use the correct form for the max and min functions as I did, or you could have used a loop. Far better to write it in one line as I did though. using a loop, I might write it as:
function [mmr,mmm] = minimaxTrial(M)
mmm = max(A,[],'all') - min(A,[],'all');
[Ar,Ac] = size(A);
mmr = NaN(Ar,1);
for ir = 1:Ar
mmr(ir) = max(A(ir,:)) - min(A(ir,:));
end
end
See why this works for any number of rows.
Also see that I pre-allocated the vector mmr in the looped version. That is something you will need to do as your code becomes more sophisticated, because you don't want to dynamically adjust the size of vectors of arrays in a loop.
Best Answer