Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > Math Resources > Mathematics Software Discussion
Reply
 
Thread Tools Display Modes
  #1  
Old October 27th, 2009, 07:08 AM
Newbie
 
Join Date: Oct 2009
Posts: 16
Country:
Thanks: 10
Thanked 0 Times in 0 Posts
Jodles is on a distinguished road
Default 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!

Last edited by Jodles; October 27th, 2009 at 07:10 AM. Reason: wrong code
Reply With Quote
Advertisement
 
  #2  
Old October 27th, 2009, 08:10 AM
Member
 
Join Date: Mar 2007
Posts: 104
Country:
Thanks: 17
Thanked 14 Times in 14 Posts
elbarto is on a distinguished road
Default

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
Reply With Quote
The following users thank elbarto for this useful post:
Donate to MHF
  #3  
Old October 27th, 2009, 09:47 AM
Newbie
 
Join Date: Oct 2009
Posts: 16
Country:
Thanks: 10
Thanked 0 Times in 0 Posts
Jodles is on a distinguished road
Default

Thank you, Elbarto!

That makes perfect sense.

All the best,
Joachim
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 05:59 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2009 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.