How can i get rid of error message'undefined varable'

I can't not get the output for matrix_max since the program keeps telling undefined variable row_max in line 17, while ive found the value for row_max in the previous loop. How can i solve that?
# function [row_max,matrix_max] = computeMatrixMax(A)
# %Input:
# %A - a is a matrix (the size is arbitrary)
# %Outputs:
# %row_max - a vector that contains the maximum value of each row
# %matrix_max - the maximum value of the matrix
# [m, n]=size(A);
# i =1;
#
# for p=2:n
# row_max=A(i,1);
# if A(1,p)>row_max
# row_max=A(1,p);
# end
# end
# if m==1;
17 matrix_max=row_max;
#
# else
# while i<m;
# i=i+1;
# rmx=A(i,1);
# for q=2:n
# if A(i,q)>rmx
# rmx=A(i,q);
# row_max=[row_max rmx];
# end
# end
# end
# end
#
# for p=2:numel(row_max)
#
# if row_max(p)>matrix_max
# matrix_max=row_max(p);
# end
# end
#
# end

2 Kommentare

ps.matlab function such as 'max' is not allowed to use to solve this function...
Make it easy for people to run your code, meaning remove all the # that you put it. Maybe then people will try it. Otherwise you can use the debugger to find out the first place you use that variable and try to figure out why you never defined that variable before that.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Thorsten
Thorsten am 22 Sep. 2015
You can compute the maximum across the rows using
max(M, [], 2)
and the global maximum using
max(M(:))

2 Kommentare

I'm required to build this function without using matlab function such as 'max'...Didn't place this requirement in the description..Sorry about that.
You can use logical indexing across rows to compute the maximum:
rowmax = R(:,1);
for i = 2:size(R,2)
idx = R(:,i) > rowmax; % logical index
rowmax(idx) = R(idx,i)
end
I leave the global max as an exercise for you.

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Noch keine Tags eingegeben.

Gefragt:

am 22 Sep. 2015

Kommentiert:

am 22 Sep. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by