find second minimum in a row in matlab without sorting

61 visualizaciones (últimos 30 días)
Nitin Sapre
Nitin Sapre el 16 de Oct. de 2019
Editada: Adam Danz el 21 de Oct. de 2019
find second minimum in a row in matlab without sorting.
excluding zero
example A = [ 3.5 2 1.6 1.456 0 1.9 2.6 ; 3.8 2.6 3.9 0 6 1.564 0 ]
  3 comentarios
Nitin Sapre
Nitin Sapre el 17 de Oct. de 2019
It's just an example
I have tried (min2) = [A,[1:pos-1 pos+1:end]
Pos is position of first minimum
Adam Danz
Adam Danz el 17 de Oct. de 2019
Why avoid sorting?

Iniciar sesión para comentar.

Respuesta aceptada

Nitin Sapre
Nitin Sapre el 19 de Oct. de 2019
[mini1(i1,1),mini1(i1,2)] = min(abs(L(i1,([1:mini(i1,2)-1 mini(i1,2)+1:end]))));
  4 comentarios
Nitin Sapre
Nitin Sapre el 21 de Oct. de 2019
yes sir ur approach is very much faster but Sir thing is L is my main matrix of size mxn i1 is variable to update rows and mini is matrix to store postion and value.
since i need to store value of 2nd min tin the positon of 1st min in a row and replace all other values with first min i used this approach.
since am beginner in matlab i dont know much about this.
Any kind of help ffrom your side will be appreciate.
thank you sir
Adam Danz
Adam Danz el 21 de Oct. de 2019
Editada: Adam Danz el 21 de Oct. de 2019
"Any kind of help ffrom your side will be appreciate"
If you have any questions about the other answers that were recommended here, please let us know. I've addapted both answers to your needs in my comment above.
Sorting may be an even better approach and it's still not clear why sorting is off the table.
An accepted answer is a sign to inexperienced users that an answer is the best solution which clearly isn't the case here. This is why you are being urged to reconsider the other options.

Iniciar sesión para comentar.

Más respuestas (2)

Ekaterina Sadovaya
Ekaterina Sadovaya el 18 de Oct. de 2019
You can exclude the first minimum. So, for example for the first row it will be
A1 = A(1,:);
[first_min_value, index] = min(A1);
A1(index) = [];
second_min_value = min(A1);
  7 comentarios
Nitin Sapre
Nitin Sapre el 19 de Oct. de 2019
why mean value?
Nitin Sapre
Nitin Sapre el 19 de Oct. de 2019
i got the ans in my soln only.
thanks for ur help and this is not my homework
for i1 = 1:m %treg(1:ti,i1)
% for col = find(L(m,:) ~= 0)
L(L== 0) = NaN;
[mini(i1,1),mini(i1,2)] = min(abs(L(i1,:))); %first minimum
% fprintf("loop ends here")
[mini1(i1,1),mini1(i1,2)] = min(i1,abs(L([1:mini(i1,2)-1 mini(i1,2)+1:end])));
its this
[mini1(i1,1),mini1(i1,2)] = min(abs(L(i1,([1:mini(i1,2)-1 mini(i1,2)+1:end]))));

Iniciar sesión para comentar.


Guillaume
Guillaume el 19 de Oct. de 2019
Use mink
A = [ 3.5 2 1.6 1.456 0 1.9 2.6 ; 3.8 2.6 3.9 0 6 1.564 0 ];
minvals = mink(A, 2, 2); %first 2 miminums along dimension 2.
minvals(:, 2) %get the 2nd one

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by