How can I get a row vector where a specific element found is located within a matrix?

3 visualizzazioni (ultimi 30 giorni)
I am trying to write a program that gives a row vector from a matrix that has the lowest element from a specific column. For example, say if I have the following,
A =
2009 8 2 4
2010 3 4 5
2011 6 9 1
2012 1 3 7
If I want to find the smallest value in column 3, which is 2, how do I get MATLAB to give the row where it is found, so that the result is
ans =
2009 8 2 4
I have been struggling with this for the past 2 days and I feel like because I've stared at this for so long I can't spot the problem. Can I get some help please?

Risposta accettata

Andrei Bobrov
Andrei Bobrov il 10 Ott 2012
Modificato: Andrei Bobrov il 10 Ott 2012
A =[
2009 8 2 4
2010 3 4 5
2011 6 9 1
2012 1 3 7];
[ii,ii] = min(A(:,3));
out = A(ii,:);
or
out = A(abs(A(:,3) - min(A(:,3))) < eps(100),:);

Più risposte (1)

Erika
Erika il 10 Ott 2012
Thank you! I do have a question, however. Why are you using ii twice? What does this do that allows to get the row vector where a specific element found is located within a matrix? I ask this so that I know next time.
  3 Commenti
Matt Tearle
Matt Tearle il 15 Ott 2012
Oh, yes, you're right -- I was a bit confused by the eps and didn't look too closely.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by