Minimum value,row and column
이전 댓글 표시
I want to find the minimum value of a matrix,the row and the column of it
답변 (3개)
[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
Giannakis Stoukas
2015년 4월 16일
Image Analyst
2015년 4월 17일
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
2015년 4월 17일
편집: Michael Haderlein
2015년 4월 17일
"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.
Image Analyst
2015년 4월 17일
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.
Giannakis Stoukas
2015년 4월 18일
Image Analyst
2015년 4월 18일
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.
Mahbubur Rahman
2016년 4월 30일
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?
Image Analyst
2016년 4월 30일
That would be row(1). That will give you the highest/topmost row in the first column where the min was located.
Mahbubur Rahman
2016년 4월 30일
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?
Image Analyst
2016년 4월 30일
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.
Mahbubur Rahman
2016년 4월 30일
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.
Image Analyst
2016년 4월 30일
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.
Image Analyst
2015년 4월 15일
Here's one way:
minValue = min(yourArray(:));
[row, column] = find(yourArray == minValue);
댓글 수: 3
Giannakis Stoukas
2015년 4월 15일
Image Analyst
2015년 4월 15일
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.
Giannakis Stoukas
2015년 4월 15일
Kaelan Wade
2017년 6월 11일
0 개 추천
Can't you just go
- A = some matrix
- [row,collum] = find(A == min(min(A)))
- min_val = S(row,collum)
댓글 수: 1
Image Analyst
2017년 6월 11일
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.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!