Thread: Avoiding having ans returned (matlab)

1. Avoiding having ans returned (matlab)

Hi!

I know a simple semicolon at the end of any command avoids output of the result of that command, but sometimes it shows up in any case. Any ideas?

Example, my function below (it returns the positions of the maximum entry in a matrix, that is *not* on the diagonal):
Code:
function [r,s] = absmax(A)

% do some error checking

% Set diagonal to 0
for k = 1:n
A(k,k) = 0;
end

% Get the largest element from each column, store elements in m and indices in i
[m, i] = max(abs(A));

% Get the largest element from m, store it in t and its position in s
[t, s] = max(m);

% Pos r is the value of elementh s in i
r = i(s);

% Return r, s
r
s

end
Any ideas why the output has both r, s and an "ans" with the same value as r? Doesn't look clean

Input on better ways of doing what I have done above is greatly appreciated!

2. start by removing this from your code:
Code:
% Return r, s
r
s
Then for the second part, if you dont specify an output variable name, the first variable of the function will be returned as "ans"

so
Code:
absmax(rand(4))
will return r as the variable "ans"

Code:
myVar_1 = absmax(rand(4))
will return r as "myVar_1" and
Code:
[myVar_1 myVar_2] = absmax(rand(4))
will return r as "myVar_1" and s as "myVar_2"

Hope that helps,

Regards Elbarto

3. Thank you, Elbarto!

That makes perfect sense.

All the best,
Joachim