9

In Matlab, you can ignore an output with the following syntax:

[~, ixMax] = max(foo);

I have a function, with signature

[out, out1, out2, out3] = function foo(in1, in2, in3)

out1, out2 and out3 are optional outputs, and each is only needed in very specific (unusual) circumstances. Foo is computationally expensive, and out1/out2/out3 are all even more computationally expensive, but rely on intermediate state generated by foo. I'd like to be able to avoid computing out1/out2/out3 if the caller is using a ~ to ignore them. How can I check for that in the definition of foo?

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
John
  • 5,735
  • 3
  • 46
  • 62
  • 3
    `nargout` is what you need. Not sure if it works if you do `[out,~,~,~]=` but it works if you do `[out]=` – Ander Biguri Oct 10 '17 at 13:32
  • 3
    @AnderBiguri is right with `nargout` - it will return 4 if the user has requested [out,~,~,~]. I dont think there is any (certainly documented) way to determine whether the user has specified a var name instead of `~` – matlabgui Oct 10 '17 at 13:34
  • @m7913d, it doesn't look like a dupe. OP asks if he can save computational resources by avoiding to call an output with `~`. – alpereira7 Oct 10 '17 at 13:58
  • 1
    @Bebs but for that he needs to determine whether an output is unused ( so whether the user used a `~`) – Irreducible Oct 10 '17 at 14:10
  • @Bebs The title is clearly the same (and correctly reflects the main question). Note that part of the answer may be in the question of the duplicate. – m7913d Oct 10 '17 at 15:13
  • Interestingly Octave has the function [isargout](https://www.gnu.org/software/octave/doc/v4.2.1/Ignoring-Arguments.html). – rahnema1 Oct 11 '17 at 03:51
  • Doh! How did I miss the dup? – John Oct 11 '17 at 14:31

1 Answers1

6

It won't accelerate the process. The ~ is a way to the reader to tell him you won't need these outputs. It also saves the memory usage of this variable.

Matlab documentation says:

However, some functions return results that use much more memory. If you do not need those variables, they waste space on your system.

So it does not improve performance because these values are internally calculated anyway.

The book Accelerating MATLAB Performance: 1001 tips to speed up MATLAB programs by Yair M. Altman says (p187):

enter image description here

However, without using the ~, and if the first output is needed, the user will gain computational time by just removing the ~ and the brackets, and writing.

out = function foo(in1, in2, in3)
alpereira7
  • 1,522
  • 3
  • 17
  • 30
  • @AnderBigurin what's wrong please? – alpereira7 Oct 10 '17 at 14:17
  • 1
    Nothing, misread some of it. It is true for the `~` operation, but one can write functions that check the amount of outputs asked, so if OP does `[out]=` then they can solve the problem – Ander Biguri Oct 10 '17 at 14:18
  • 2
    @AnderBiguri, yes indeed, my answer only focuses on the `~` part. If only the first output is needed, you are correct. – alpereira7 Oct 10 '17 at 14:20
  • I am confused by the answer. The reason is, the OP ask how to avoid unnecessary computation by checking whether a output is used. So I dont see how this answer addresses this question? – Irreducible Oct 10 '17 at 14:22
  • 1
    @Irreducible it answers the case the OP asks for, when the input is *asked*, but not stored, i.e. `~`. The function does not know if the output is stored, only that is being called. But yes, I think the answer shoudl more clearly state this – Ander Biguri Oct 10 '17 at 14:24
  • @AnderBiguri, I thought the OP asks to check _if the caller is using a `~`_ . So he asks for a way to check whether an output is set to `~` and internally use this information to avoid certain computations?!? For me it seems so that the OP understands that Matlab interprets `~` as an output in general. – Irreducible Oct 10 '17 at 14:28
  • @Irreducible and this answer says: you can not do that. – Ander Biguri Oct 10 '17 at 14:29
  • In m7913d link it seems so that there is a way to over come this? [istilde](http://biorobots.cwru.edu/personnel/adh/stackoverflow/04/) – Irreducible Oct 10 '17 at 14:31
  • 1
    @Irreducible, Matlab says that with the `~` the output is still calculated, but not stored. – alpereira7 Oct 10 '17 at 14:37
  • Sorry perhaps I am completely wrong. However, my understanding is, that the OP knows the fact that Matlab interprets `~` as an output. I am also aware of this. However, from my perspective the OP ask whether he can check if an output is set by the caller to `~`. m7913d shared a link where someone claims he wrote a function to do so. – Irreducible Oct 10 '17 at 14:41
  • @Irreducible, alright, I understand your point. The code you provided is interesting. – alpereira7 Oct 10 '17 at 14:43
  • 3
    Nice answer! Just a warning: the quoted text from Altman's book says _trailing `~` signs should be removed from the code whenever they appear_. That's not always true. Some functions return a different first output when they are called with two outputs. Compare for example `a = find(1:3)` and `[a,~] = find(1:3)` – Luis Mendo Oct 10 '17 at 14:54
  • 1
    @LuisMendo, I agree, you have a good example of a function that has a different behaviour depending on how many outputs are asked so `~` **is** needed sometimes :-) – alpereira7 Oct 10 '17 at 15:04
  • 1
    @LuisMendo - I stand corrected, there are indeed cases such as you describe, so I will add a cautionary note to the 2nd edition of my book. Having said that, I venture to say that these cases are not very common and IMHO demonstrate bad programming design practice. I believe that extra output args, just as extra input args, are best used as purely optional args that have no effect on the meaning of the preceding args. – Yair Altman Dec 21 '17 at 21:28
  • @YairAltman I totally agree. They are not very common (the only other function I can think of right now is `size`) and they can be confusing, so it's better to avoid that as programming practice – Luis Mendo Dec 21 '17 at 21:59