Find max of matrix without using built in function.

I want to find the "max(a)" without actually using the built in "max" function.

2 Kommentare

Roland
Roland am 4 Mai 2011
I'm trying to do this with a for loop as test preparation.
We won't do your homework for you. What have you tried so far.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Paulo Silva
Paulo Silva am 4 Mai 2011
mx=a(1);
for p=2:numel(a)
if a(p)>mx
mx=a(p);
end
end
mx %the maximum value

3 Kommentare

Roland
Roland am 4 Mai 2011
How would I find the max of each column individually?
%code made here, not tested in matlab
mxc=zeros(1,size(a,2));
for n=1:size(a,2)
mx=a(1,n);
for p=2:size(a,1)
if a(p,n)>mx
mx=a(p,n);
end
end
mxc(n)=mx;
end
mxc %the maximum value per column
The code isn't optimized on purpose, like Sean said "We won't do your homework for you"

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Sean de Wolski
Sean de Wolski am 4 Mai 2011
the_max = -min(-(a(:)))

2 Kommentare

Roland
Roland am 4 Mai 2011
I need to use logical operators.
the_max = a(a==-min(-(a(:))))
There!

Melden Sie sich an, um zu kommentieren.

Adrien Leygue
Adrien Leygue am 4 Mai 2011

0 Stimmen

The following code will extract the minimum over each column. No loop no max no min and of course not optimal. Feel free to adapt it to other purposes:
S = A((repmat(eye(size(A,1)),[1 size(A,1)]) *(kron(A,ones(size(A,1),1)) < kron(ones(size(A,1),1),A)))==0)'
A.

Kategorien

Mehr zu MATLAB 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!

Translated by