How do i call function with three output arguments?

I have an assignment to write a function which returns the minimum value of the element in the matrix and his index . If function is called with two output arguments , it is necessary to return the minimum value and his linear index If the function is called with three output arguments , also min but with his matrix index. Also i cannot use syntax such as min and others. Problem is that he finds min but he sees only firs value if there's more than one min and i cannot get those linear and matrix indexes.
function [m, v, k] = minimum(A)
m = A(1,1);
v = 1;
k=1;
for i = 2: size(A,1)
for j = 2 : size(A,2)
if A(i,j) < m
m = A(i,j);
v = i;
k=j;
end
end
end
if nargout == 2
v = (k - 1) * size(A,1) + v;
end
if nargout==3
k=ceil(v/size(A,1));
v=v-(k-1)*size(A,1);
end

2 comentarios

I am not getting your problem
Azzi Abdelmalek
Azzi Abdelmalek el 7 de Mayo de 2016
Editada: Azzi Abdelmalek el 7 de Mayo de 2016
What is N in your program?
N=size(A,1)

Iniciar sesión para comentar.

Respuestas (2)

You omit to define N
N=size(A,1)
and your first for loop should begin at k=1
for i = 1: size(A,1)

3 comentarios

Beginner
Beginner el 7 de Mayo de 2016
typing mistake with "N".We just started learning this program, so im new to this.I dont know how to test it so see if it works with when program has three or two output arguments.
Azzi Abdelmalek
Azzi Abdelmalek el 7 de Mayo de 2016
Editada: Azzi Abdelmalek el 7 de Mayo de 2016
If you are looking for all min value, you can use min function with another line of code.
val=min(y)
[ix,iy]=find(y==val)

Iniciar sesión para comentar.

Jan
Jan el 7 de Mayo de 2016
Editada: Jan el 7 de Mayo de 2016
function [minValue, index1, index2] = minimum(A)
minValue = A(1, 1);
index1 = [];
index2 = [];
for i2 = 1:size(A, 2)
for i1 = 1:size(A, 1)
if A(i1, i2) < minValue
minValue = A(i1, i2);
index1 = i1; % Remember new indices
index2 = i2;
elseif A(i1, i2) == minValue % Append to indices
index1 = cat(2, index1, i1);
index2 = cat(2, index2, i2);
end
end
end
if nargout == 2
index1 = index1 + (index2 - 1) * size(A, 1);
end

3 comentarios

Beginner
Beginner el 7 de Mayo de 2016
When i have more than one min in matrix he wont show index where those min are.That's the main problem what i have in this assignment.Also thank you.
You have to basically do what Jan says twice. Once to find the overall min value, then again to find out all the places it occurs.
You can do it all in one loop if you're clever about it, like resetting the "indexesOfMins" variable that you need to build up each time you encounter a new minimum. You'd also need to concatenate new indexes to that if you ever encounter another value that has exactly the same value as the current min value. See if you can figure it out. If not, the easy no-brainer way is to just do the loop twice, the second time looking for the overall min that you found in the first loop.
@Beginner and Image Analyst: My code replies the indices of all values already, which equal the minimum:
a = [3, 3, 4, 4; ...
4, 1, 2, 2; ...
2, 1, 3, 3; ...
3, 2, 1, 3];
[value, pos] = minimum(a)
% value: 1
% pos: 6, 7, 12

Iniciar sesión para comentar.

Categorías

Preguntada:

el 7 de Mayo de 2016

Editada:

Jan
el 7 de Mayo de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by