How can I use a nested for loop to find multiple minimum values in a matrix array?

4 views (last 30 days)
I am looking to write a script that will determine what the minimum value in a matrix is, and where it is located (it occurs more than once). I have written a code that will find a single minimum, but I'm struggling to adapt it for more than one minimum. This is a homework so any hints would be greatly appreciated. here is my code so far for matrix A.
A=[12,44,7,32,3;66,32,3,55,9;14,53,28,3,5];
MinVal=A(1,1);
RowVal=1;
ColVal=1;
for i=1:3
for j=1:3
if A(i,j)<MinVal;
MinVal=A(i,j);
RowVal=i;
ColVal=j;
end
end
end
formatspec='The minimum value is %0.2f in row %0.0f and column %0.0f.\n';
fprintf(formatspec,MinVal,RowVal,ColVal)

Accepted Answer

Guillaume
Guillaume on 13 Oct 2015
Edited: Guillaume on 13 Oct 2015
One possible way is to have RowVal and ColVal as vectors instead of scalars. You then have two if conditions:
  • A(i,j) == MinVal : add current row and column to the end of the RowVal and ColVal vectors
  • A(i,j) < MinVal : update MinVal and reset RowVal and ColVal
That should be enough for you to write the code.
As an aside, avoid hardcoding the end values of your loop. Use
for i = 1:size(A, 1) %similar for j
That way the code works even if you change the size of A.
  3 Comments
Thorsten
Thorsten on 14 Oct 2015
Edited: Thorsten on 14 Oct 2015
Guillaume told you to add current row and column to the end of the RowVal and ColVal vectors if A(i,j) == MinVal. You have just updated RowVal and ColVal. See my answer on how to add values.
Guillaume
Guillaume on 14 Oct 2015
There are several issues with your code:
  • you're indexing RowVal and ColVal with the row and column. That makes no sense.
  • the second if is never going to be true as it is embedded in the first if. That is you can only ever reach if A(i,j)==MinVal iif the first if is true and that's only when A(i,j)<MinVal.
  • Even if that second if could be executed it doesn't do what it should: "reset RowVal and ColVal"
To fix the first one, create an additional variable that you only increase when you've found a minimum and use that variable as index. Alternatively, use end+1.
To fix the second one, use elseif
To reset a variable that was previously a vector, either:
%var = [1 2 3 4 5]; %vector to reset
var = [];
var(1) = value
or:
%var = [1 2 3 4 5]; %vector to reset
var = value

Sign in to comment.

More Answers (2)

Thorsten
Thorsten on 14 Oct 2015
Edited: Thorsten on 14 Oct 2015
Simplify your code by using a linear index
ind = 1;
minval = A(ind);
for i =1:numel(A)
if A(i) < minval
minval = A(i);
ind = i;
elseif A(i) == minval
ind = [ind i]; % add current index
end
end
Convert linear index to sub scripts:
[i j] =ind2sub(size(A), ind)

Stephen23
Stephen23 on 14 Oct 2015
Edited: Stephen23 on 14 Oct 2015
It is easy to get the row and column indices of the minimum without any loops using code vectorization. This is faster and much more efficient than using nested loops:
>> A = [12,44,7,32,3;66,32,3,55,9;14,53,28,3,5];
>> X = min(A(:));
>> [R,C] = find(A==X)
R =
2
3
1
C =
3
4
5
One can also display the text without any loops:
>> fmt = 'The minimum value is %0.2f in row %0.0f and column %0.0f.\n';
>> fprintf(fmt,[X*ones(size(R)),R,C].')
The minimum value is 3.00 in row 2 and column 3.
The minimum value is 3.00 in row 3 and column 4.
The minimum value is 3.00 in row 1 and column 5.
  3 Comments
Guillaume
Guillaume on 14 Oct 2015
As it's homework I assume the OP has to use a loop. Otherwise, yes, a loop is pointless.
Stephen23
Stephen23 on 14 Oct 2015
I realize this is probably homework and restricted to loops, but in case someone is browsing Answers or using an internet search engine then there needs to be atleast one answer that gives a more efficient solution to this problem, and links to the reasons why using loops is a poor solution.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!