0

I am using a function with multiple outputs in Matlab, but am only interested in one of the outputs. I would like to suppress the other output variables (i.e. avoid them being returned and placed into memory). For example, with the max function:

[output1 output2] = max(matrixA, [], 1);
% output1 returns the maximum, which i'm not interested in
% output2 returns the index of the maximum, which i *am* interested in

Is there any way to call the function so that output1 is not returned? And if there is, does it offer any memory advantage over calculating as above but immediately calling clear output1 to remove output1 from the memory?

Thanks for your help.

Amro
  • 123,847
  • 25
  • 243
  • 454
3lbom
  • 139
  • 1
  • 1
  • 11
  • possible duplicate of [How to elegantly ignore some return values of a MATLAB function?](http://stackoverflow.com/questions/747296/how-to-elegantly-ignore-some-return-values-of-a-matlab-function). Also [ignoring return value syntax?](http://stackoverflow.com/q/5177229/97160) and [MATLAB black hole variable](http://stackoverflow.com/questions/5407465/matlab-black-hole-variable) – Amro May 27 '12 at 21:40
  • 1
    @Amro The only difference seems to be the mention of performance considerations, though as far as I can tell there are none. – reve_etrange May 27 '12 at 21:42
  • 3
    @reve_etrange: I guess if you really want to avoid unnecessary computation, you will have to design your function to return `varargout` arguments, and rewrite it to only do the computation if enough output arguments were passed (determined with the `nargout` value) – Amro May 27 '12 at 21:51
  • @Amro Fortunately that approach is not difficult. It's too bad, though, the JIT compiler seems like a great place to do that. – reve_etrange May 29 '12 at 20:05

2 Answers2

5

Use the tilde:

[~, output2] = max(matrixA, [], 1);

I doubt there would be much memory advantage (apart from clerical stuff like allocating output variables, etc.)) since the function will run completely and allocate all that it needs to. This way, you just don't get the value, and the value of the first output variable in the scope of the max function will be garbage-collected.

Ansari
  • 8,168
  • 2
  • 23
  • 34
  • 3
    Remark that this is will not work under older versions of matlab. I believe this is implemented since about R2009b... – Gunther Struyf May 27 '12 at 21:37
  • Wasn't aware of that - thank you for pointing it out! What did people do prior to 2009b? – Ansari May 27 '12 at 21:39
  • See this [question](http://stackoverflow.com/questions/747296/how-to-elegantly-ignore-some-return-values-of-a-matlab-function) (of which this seems to be a duplicate). One of the answers there has an alternative. – reve_etrange May 27 '12 at 21:40
  • @Ansari Use a variable called DUMMY or something similar – learnvst May 27 '12 at 21:40
  • @Ansari: what OP did^^ clearing unwanted variables after the call – Gunther Struyf May 27 '12 at 21:41
  • @learnvst Or use `ans` (as recommended in one the linked question above). – reve_etrange May 27 '12 at 21:41
  • Thanks. In case anyone's wondering, the same tilde functionality is also available in Octave from version 3.4.x onwards. (much easier to search something when you already know what you're looking for!) Thanks for the help. – 3lbom May 27 '12 at 21:45
2

Replace any output variables you don't want with a ~ character.

E.g.

[~,I] = max(matrix);

This pattern has an advantage over clear in that the MATLAB interpreter and just-in-time compiler can avoid the memory and CPU costs of calculating ignored variables.

Edit

Here is the documentation and a blog post by Loren Shure on this use of ~. I can't find any definite information about use of ignored variables for eliminating unnecessary computation.

reve_etrange
  • 2,561
  • 1
  • 22
  • 36