How to find matrix column & row max & min output as vector?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
David Hughes
am 7 Jul. 2015
Bearbeitet: James Tursa
am 7 Jul. 2015
I need to create a script that gives the maximum & minimum value of each column and each row of a vector. This is what I have so far for the overall max. The answer is supposed to display the the answer as a vector. This is my vector a=[0 7 3 -1 4; 12 14 8 9 5; -2 -3 -4 -7 -6;3 1 9 12 8]. I used the same code for overall max & min. I'm stuck on looping through the columns and giving the answer for each.
mx=a(1);
for p=2:numel(a)
if a(p)>mx
mx=a(p);
end
end
mn=a(1);
for p=2:numel(A)
if A(p)<mn
mn=A(p);
end
end
[mx mn]
0 Kommentare
Akzeptierte Antwort
James Tursa
am 7 Jul. 2015
Bearbeitet: James Tursa
am 7 Jul. 2015
For a matrix, use two indexes to separate the logic for the columns (or rows). E.g., for the column max values using a variation of what you already have:
[nr nc] = size(a); % number of rows & columns
mxc = zeros(1,nc); % initialize a max column vector
for c=1:nc % loop over the columns
mxc(c) = a(1,c); % initial max value
for r=2:nr
% put code here for testing & updating the max value
end
end
Then adjust the above for min of columns, max of rows, and min of rows.
0 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!