user321277
user321277

Reputation:

Why is "sum A" not the same as "sum(A)" in MATLAB?

Why is it that sum A is not the same as sum(A) in MATLAB?

>> A

A =

     1     2
     3     4

>> sum A

ans =

    65

>> sum(A)

ans =

     4     6

A more general question would be: why is sum A working at all?

Upvotes: 7

Views: 104

Answers (1)

kamjagin
kamjagin

Reputation: 3654

If you call a function like

sum A

Matlab interprets the second as a string and passes it as the first argument to the function like:

sum('A')

And the output 65 comes from 65 being the integer representation of 'A'

This is quite neat with the large number of functions taking strings as input, like cd Instead of

cd('somedirectory/')

you can write

cd somedirectory/

This is referred command syntax and functions called like this cannot have outputs

Here is a link with some additional details: (http://www.mathworks.se/help/matlab/matlab_prog/command-vs-function-syntax.html)

Upvotes: 11

Related Questions