How can i get rid of error message'undefined varable'
Ältere Kommentare anzeigen
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
Hanzhi Zhang
am 22 Sep. 2015
Image Analyst
am 22 Sep. 2015
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.
Antworten (1)
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
Hanzhi Zhang
am 22 Sep. 2015
Thorsten
am 22 Sep. 2015
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.
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!