0

I'm trying to directly retrieve the second variable of a function that returns multiple variables. For example, I have the column vector a as follows:

a = [5 ; 4 ; 3 ; 2 ; 1 ; 9 ; 8 ; 7];

And I want to retrieve the index of the minimum value. I know I can do this.

[n,i] = min(a);
i

But how can I do this essentially in one line? I thought this ould work but it does not:

min(a)(1)
Hurricane Development
  • 2,449
  • 1
  • 19
  • 40
  • 2
    What you describe is called 'chaining'. Octave supports this syntax, but matlab does not. There are weird ways you could get that effect (e.g. specifying a 'paren' function which essentially internally does what you want, as the one described here [here](https://stackoverflow.com/q/39628157/4183191)), but usually they reduce rather than improve readability. – Tasos Papastylianou Jan 05 '18 at 18:23
  • This is not a duplicate of https://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it That question asks to index into the first output variable. This question asks to access the second output variable. These are similar, but different questions. – Cris Luengo Jan 06 '18 at 05:38
  • However, this question is an identical duplicate of this other question: https://stackoverflow.com/q/3710466/7328782 – Cris Luengo Jan 06 '18 at 05:51

2 Answers2

0

You can ignore one of them like this:

[n,~] = min(a);

Or:

[~,i] = min(a);
OmG
  • 18,337
  • 10
  • 57
  • 90
0

It's not pretty, but it might work for you:

find( x == min(x), 1, 'first' )
AnonSubmitter85
  • 933
  • 7
  • 14