finding minimum value of certain row of matrix with condition
21 views (last 30 days)
Show older comments
i have matrix [6,6], how to find minimum value at certain row eg what is the minimum value that >0, at row 1?
r=[0.00 18.68 39.62 20.81 5.39 39.92
18.68 0.00 33.84 38.91 24.04 53.41
39.62 33.84 0.00 48.38 42.20 45.12
20.81 38.91 48.38 0.00 15.62 25.08
5.39 24.04 42.20 15.62 0.00 36.40
39.92 53.41 45.12 25.08 36.40 0.00];
Then the result is 5.39 in [1 5], it will loop to continued to find the min value at other row, the next row searching on the min value is based on the previous result of the column. eg 2nd result is 15.62 [5 4]
2 Comments
Mohammad Abouali
on 15 Oct 2014
what would be the third result? you jumped from row 1 to row 5. The result in row one was 5.39 and the first element in row 5 is 5.39. But now the result is 15.62 but there is no row with 15.62 as the first value. so what would be the third result? or do you stop here.
Accepted Answer
Image Analyst
on 15 Oct 2014
If I understood you correctly, try this:
clc;
workspace;
fontSize = 25;
close all;
r=[0.00 18.68 39.62 20.81 5.39 39.92
18.68 0.00 33.84 38.91 24.04 53.41
39.62 33.84 0.00 48.38 42.20 45.12
20.81 38.91 48.38 0.00 15.62 25.08
5.39 24.04 42.20 15.62 0.00 36.40
39.92 53.41 45.12 25.08 36.40 0.00];
[rows, columns] = size(r)
thisMin = -1*ones(rows, 1);
priorRowMin = 0;
for row = 1 : rows
% Get this row all by itself.
thisRow = r(row, :);
% Remove values less than or equal to the prior row's min
% because we only want to consider values above the prior row's min.
thisRow(thisRow <= priorRowMin) = [];
if ~isempty(thisRow)
% If there are any values left...
% Find the min of what's left.
thisMin(row) = min(thisRow);
% Update the prior row min:
priorRowMin = thisMin(row);
end
end
thisMin
In the command window:
thisMin =
5.39
18.68
33.84
38.91
42.2
45.12
2 Comments
Image Analyst
on 16 Oct 2014
Before the loop do
locations = -1*ones(rows, 2);
Inside the if, do
[thisMin(row), columnOfMin] = min(thisRow);
locations(row, 1) = row; % Save row
locations(row, 2) = columnOfMin; % Save column.
More Answers (1)
Sean de Wolski
on 15 Oct 2014
Replace the zeros with Infs and take the min:
r2 = r;
r2(r2==0) = inf;
min(r2,[],2)
0 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!