What is the point of the deal() function in Octave / MATLAB?

丶灬走出姿态 提交于 2019-12-01 21:34:47

One really useful way that I occasionally use deal is to create anonymous functions that return multiple output arguments. For example,

>> f = @(x) deal(x^2,x^3);
>> [a,b] = f(3)
a =
     9
b =
    27

Edit since people seem to find this pattern potentially useful, do note a quirk, in that you must return the full number of outputs. In particular, you can't use a = f(3), or it will error. To retrieve just a single output, use [a,~] = f(3) or [~,b] = f(3). The ~ syntax for suppressing output arguments has been around since about R2007a or so (I'm afraid I can't recall exactly when) - in older versions, you will need to always return both outputs. Hope that may be useful for you.

First, your named examples work with octave, but not with matlab!

octave:1> a=b=c=42
a =                   42
octave:2> [a,b,c] = {1,2,3}{:}
a =                    1
b =                    2
c =                    3


>> a=b=c=42
 a=b=c=42
    |
Error: The expression to the left of the equals sign is not a valid target for an assignment.

>> [a,b,c] = {1,2,3}{:}
 [a,b,c] = {1,2,3}{:}
                  |
Error: Unbalanced or unexpected parenthesis or bracket.

How and when to use deal() can not be generalized. It depends on you scripte, your datastructure, what you want to do and how the result should look like etc.

>> m=rand(3,3);
>> [a,b,c]=m;
Too many output arguments.

>> [a,b,c]=reshape(m,[],1);
Error using reshape
Too many output arguments.

>> [a,b,c]=deal(reshape(m,[],1));
>> 

So some functions are designed with nargout = 1 and you can easily distribute the output to sevaral variables in "one line".

And if a functions has more nargouts than 1, it gets interesting

>> [x,y,z]=qr(m)

x =

   -0.7004    0.6471   -0.3012
   -0.1144   -0.5183   -0.8475
   -0.7045   -0.5592    0.4370


y =

   -1.3776   -0.7928   -1.2897
         0   -0.6388   -0.0796
         0         0   -0.3499


z =

     1     0     0
     0     0     1
     0     1     0

>> [x,y,z]=deal(qr(m))

x =

   -1.3776   -1.2897   -0.7928
    0.0673   -0.3588   -0.1418
    0.4143   -0.1886    0.6229


y =

   -1.3776   -1.2897   -0.7928
    0.0673   -0.3588   -0.1418
    0.4143   -0.1886    0.6229


z =

   -1.3776   -1.2897   -0.7928
    0.0673   -0.3588   -0.1418
    0.4143   -0.1886    0.6229

>> 

However, doublicating variables (e.g. to keep a better overview) is not so bad as it might be in other languages (in regard of memory wasting). Matlab uses copy on write - see https://en.wikipedia.org/wiki/Copy-on-write and http://www.matlabtips.com/copy-on-write/

And - afaik - octave too:

octave:1> memory

 Memory used by Octave:   24.4414 MB 
 Physical Memory (RAM): 7586.74 MB 

octave:2> tic, m=rand(10000,10000); toc
Elapsed time is 5.52749 seconds.
octave:3> memory

 Memory used by Octave:   787.531 MB 
 Physical Memory (RAM): 7586.74 MB 

octave:4> tic, n=m; toc
Elapsed time is 4.19617e-05 seconds.
octave:5> memory

 Memory used by Octave:   787.535 MB 
 Physical Memory (RAM): 7586.74 MB 

octave:6> whos
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  ===== 
        m       10000x10000              800000000  double
        n       10000x10000              800000000  double

Total is 200000000 elements using 1600000000 bytes
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!