Minimum value,row and column

I want to find the minimum value of a matrix,the row and the column of it

Answers (3)

Jan
Jan on 16 Apr 2015
Edited: Jan on 16 Apr 2015
[value, index] = min(A(:));
[row, col] = ind2sub(size(A), index);
In opposite to the solution of Image Analyst, this is faster, but considers only one value even if the minimal value appears multiple times in the array.

12 Comments

This is what i want,only one value.Because I dont understand the command,i have a matrix "x" and i want to save the value to "min_value". Which is the way to write this command?
If the min value occurs at, say, 10 places in your matrix, which of the 10 locations do you want returned? If you know for a fact that there is only one , then you can use either Jan's or my solution - the difference is how to handle it when the min occurs at multiple locations. Do you want one (if so, which one) or all of them. It sounds like you have a situation where the min may occur many times but we still don't know which of them you want.
Michael Haderlein
Michael Haderlein on 17 Apr 2015
Edited: Michael Haderlein on 17 Apr 2015
"This is what i want,only one value." So you can use Jan's solution. The minimum value is the first output argument of min(), as Jan's variable naming already suggests. You can of course rename Jan's value with min_value.
The best for you would be to just create a simple matrix, like magic(4), and go through both Jan's and Image Analyst's code step by step. You'll learn about indexing which is one of the most important skills to have for writing efficient Matlab codes.
magic has only 1 min so Jan's and my code will return the same location. Next, it would be instructive to pick a matrix with multiple min locations, like
>> randi(5, 6, 4)
ans =
3 4 1 4
3 3 4 2
5 2 2 4
3 1 3 4
2 3 4 3
4 2 2 1
to illustrate the differences between my code and Jan's code. Jan's code will pick the 1 in row 4, column 4 (because MATLAB searches in a top to bottom, left to right order) whereas my code will return an array of all 3 locations where you need to pick the single min location that you want.
row =
4
1
6
column =
2
3
4
Perhaps if you tell us the criteria for picking your single min, we could help further.
It is a part of a large code i am making and i cannot explain all the logic above this.When i have one minimum value in many rows/columns i just want one of them randomly,because i need to use the row and column of it later in the code and it has no matter which of the possible row/column i have.
Now you've said that you have only one min value, which can be located anywhere. This is different than saying that there is one min value but it may exist in several different locations but you want just one of those locations. So in that case, you can use either solution - they'll both work in the case of a single min in a single location.
In the above example there are 3 minimum values. What do I need to do to select the minimum value with the lowest column number? I mean how can I get the row number, using the column number 2?
That would be row(1). That will give you the highest/topmost row in the first column where the min was located.
No. The row with the lowest column number is row 4 (row 4, col 2 has the min value). My question is that how can you get this value of the row using a code?
Why do you say no??? Look, from my example above which you referenced (I'm copying here):
row =
4
1
6
And I said the number you want would be row(1). Well the variable row = [4;1;6]. Which means row(1) = 4, row(2) = 1, and row(3) = 6. You say you expect 4, and row(1) gives you 4, so what's the problem? Do you just not know how to assign it to a variable "in code"? If so, do something like this:
thisValueOfTheRow = row(1);
Of course I called the variable "thisValueOfTheRow" but you can call it whatever you like. Note that thisValueOfTheRow will equal 4.
row(1) is the first row in the leftmost column where the criteria (which is yourArray == minValue) is met:
minValue = min(yourArray(:));
[row, column] = find(yourArray == minValue);
row and column are computed in column major order - in other words down the rows, then over column-by-column.
Please clarify how row(1) is not the value you're looking for because I guess I'm not understanding what you want then.
Hi, thanks again. Your answer is okay. But problem is how do I relate the lowest row for the lowest column.
A = [1 1 7 1 8; 2 4 5 9 5; 6 5 0 2 3; 3 7 5 1 9; 9 5 2 6 7]
[r c]= find(A==5);
You can see that the lowest number of c is 2. And for this c = 2, we get two r (3 and 5). Now I want to select the r_min for c_min (which is c=2). My answer should be (2,3). Just figured out that If I make a matrix
B=[c r];
Answer = B(1,:)
It gives me the desired value. Comment please.
It's the same solution I've been telling you. Now I've renamed minA to valueToFind since it appears that you're not always looking to find the min values. And since you want an (x,y) answer rather than "value of the row" like you asked for before I just concatenate them together:
A = [1 1 7 1 8; 2 4 5 9 5; 6 5 0 2 3; 3 7 5 1 9; 9 5 2 6 7]
% valueToFind = min(A(:)); % Find the min value of A
valueToFind = 5;
[row, column] = find(A == valueToFind)
MyAnswer = [column(1), row(1)] % In format [x,y] NOT [row, column]
You can call it MyAnswer, or B, or whatever you want, but I'd probably not use Answer.

Sign in to comment.

Here's one way:
minValue = min(yourArray(:));
[row, column] = find(yourArray == minValue);

3 Comments

Another way?
You could run down row by row inverting the lines and then use findpeaks() in the Signal Processing Toolbox. Probably far less efficient than the first way.
You could use imregionalmin() in the Image Processing Toolbox.
There are other ways I'm sure, such as functions in the various optimization toolboxes.
I will do it the easy way then,thanks

Sign in to comment.

Kaelan Wade
Kaelan Wade on 11 Jun 2017
Can't you just go
  • A = some matrix
  • [row,collum] = find(A == min(min(A)))
  • min_val = S(row,collum)

1 Comment

Yes, you can. In fact that's what my answer up above already said, though in a more efficient way. You don't need to use min twice if you use (:) and your way gives an array of all the same min value whereas most likely only a single value is needed, even if the min shows up multiple times.

Sign in to comment.

Asked:

on 15 Apr 2015

Commented:

on 11 Jun 2017

Community Treasure Hunt

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

Start Hunting!