MATLAB: Function output getting ‘index exceeds matrix dimension’

matrix

for my output [row_max matrix_max], the format of my row_max is a vector (1 x no. of rows) and my matrix_max should be the maximum in my row_max which only have one. When I delete the matrix_max the whole function seems working fine, which is returning a single vector. If I add matrix_max in, then the index exceeds matrix dimension comes out.

Best Answer

It is never a good idea to use common matlab function names as variable names. In the line
matrix_max = max(row_max);
You're either trying to call the max function. But because you've created a variable max, you're actually indexing your max variable. Considering that earlier you're manually computing the maximum instead of using the max function, it's not clear whether you now decided to use the built-in max.
Or you're actually trying to index your max variable, which does not make sense since you never put more than one element in it. The only valid index is thus 1. Whenever row_max is greater than 1, you'll get index exceeds matrix dimension.
The whole function could be replace by:
function [row_max, matrix_max] = computeMatrixMax(A) %misleading function name!
row_max = max(A, [], 2);
matrix_max = max(row_max);
end