How could this be achieved in one line of code?

My professor suggested it's possible to do the following with a single line of code:
From some 2-D matrix "A", pull out the row numbers of the minimum value in each column, and store them in "OutVec". For example, given the matrix
A =
43 40 73 32 92 91
56 91 78 6 25 78
78 65 35 30 24 43
The expected output would be
outVec =
1
1
3
2
3
3
I am almost able to get there with
find(A == min(A, [], 1))
which returns
ans =
1
4
9
11
15
18
But of course these are linear indeces, and I'm looking only for row numbers. I thought about taking
rem(find(A == min(A, [], 1)), 3)
but then of course everything in the output that should be a 3 becomes a 0.
This is part of an assignment, but only as a challenge. It's not graded differently whether it's done in one line or multiple, and I could definitely do this in two or three lines. I'm just curious how it could be done in one. Thanks.

4 Comments

Actually, I realized I can make
find(A == min(A, [], 1))
work simply by adding some throwaway output variable:
[outVec, trashVec] = find(A == min(A, [], 1))
This fills "outVec" with row numbers and "trashVec" with column numbers. Almost sorry I wrote this whole question when the solution was right in front of me.
Actually, the min function by itself will give you what you want - you don't need to use the find function -
A =[43 40 73 32 92 91;
56 91 78 6 25 78;
78 65 35 30 24 43]
[~,index]=min(A)
index =
1 1 3 2 3 3
That's an amazingly handy shortcut!
Can you explain to me what is going on here syntax-wise? I didn't see anything like this in the documentation for min.
Edit: Ohhh, I see. The ~ is just a placeholder argument. I hadn't been exposed to that before. Thanks!

Sign in to comment.

 Accepted Answer

[~,idx] = min(A,[],1)

It helps to read the documentation for every operator and function that you use. In the MATLAB documentation the syntaxes are always listed at the top of the help page. The help for min lists this syntax:

" [M,I] = min(...) finds the indices of the minimum values of A and returns them in output vector I..."

The I is a clickable link, which takes your further down the page... it explains what I is. Sadly the help is a bit vague on how to interpret it, but some experimentation makes it clear that the index is along the same dimension being operated along.

3 Comments

Just out of interest, what is the actual purpose of the empty square brackets in the function call? Maybe I haven't read the documentation carefully enough, but I've can't say I've noticed anything that explains what use they serve.
@David Fletcher: the empty brackets are a sign of MATLAB's venerable age, and a time when programming language fashion was very different to today.... Unlike more modern languages where input arguments can be specified by name, in MATLAB all input/output arguments are positional. Because the position is critical, many MATLAB functions allow [] as a kind of "ignore this position" indicator, so that more positional inputs may be entered. In the case of min (and max) the function actually combines two similar but different functionalities:
  1. minimum within one array (as used in this question).
  2. minimum values from two arrays (element-wise).
In the help these are shown at the top of the page using different syntaxes:
  1. min(A) or min(A,[],...)
  2. min(A,B)
You can see that the first functionality offers the option of specify the dimension operated along, but because the second functionality requires two input arguments and has thus defined what the second input is for, a third input argument is therefore used to specify the dimension argument. So for this question we want to use the first functionality and also specify the dimension (to make it more robust code): therefore supply the first input as the array itself, and the third input as the dimension... leaving the second to be filled by a placeholder [], just as the min help shows.
Note that it would be ambiguous to use the second argument for the dimension, as would this example:
min(1:2,1)
be interpreted as the first functionality (min value of first input along dimension 1), or as the second functionality (element-wise min of two arrays, one of which happens to be a scalar)?
Which functions support the [] placeholder is explained in their help: always read the help for every operator and function that you use, not matter how trivial you might think it is.
Note that in some other languages the two functionalities are considered as different functions, e.g. in numpy they are amin and minimum respectively.
OK, thanks for taking the effort to answer the query.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!