finding minimum value of certain row of matrix with condition

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 Kommentare

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.
nadhirah
nadhirah am 16 Okt. 2014
Bearbeitet: nadhirah am 16 Okt. 2014
no, it should be loop till complete all the 6 points. i think i should put the others condition such as only visited once and end with point 1 and each point only visited once.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

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 Kommentare

may i know how can i display the current coordinate of the min point, such as 5.39 coordinate is [1 5].
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Replace the zeros with Infs and take the min:
r2 = r;
r2(r2==0) = inf;
min(r2,[],2)

Kategorien

Mehr zu Mathematics and Optimization 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