# How to find the two minimum values ?

9 Ansichten (letzte 30 Tage)
Yan Ket am 22 Jun. 2021
Kommentiert: Yan Ket am 23 Jun. 2021
Hello!
i found a problem. Try to find the minimum position of both values. mycode:
col1 = x(:,1);
col2 = x(:,2);
n = 2;
val = zeros(n,1);
fr = zeros(n,1);
idA = zeros(n,1);
for i=1:n
[val(i) , idA(i)] = min(col2); % Find the minimum and position of col2.
col2(idA(i)) = []; % Remove duplicates in first iteration in col2
end
fr = col1(idA);
final. I got the minimum 2 values but I didn't get the correct position.I got the same position from the last iteration.What could I do (to solve this)?
(I need the position in order to retrieve the values in the col1 variable.)
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Joseph Cheng am 22 Jun. 2021
well to remedy this you shouldn't "delete" the entry of the min point but stick in a large value or Nan and use nanmin(). by deleting the minimum in the for loop you're changing the indexing positioning each time as the order of the index is different and gets shorter with each loop. force the value to be huge or nan and it'll keep the correct index for the found minimum index
##### 2 KommentareKeine anzeigenKeine ausblenden
Joseph Cheng am 22 Jun. 2021
oh and additionally your col2(idA(i)) isn't removing duplicates its just deleted the first found min position. to delete duplicates you should use
col2(find(col2==val(i))) =nan or inf
as that'll first find the duplicate min values and sub the values
Yan Ket am 23 Jun. 2021
I’m really grateful for your help.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help 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