with and without bracket
显示 更早的评论
SD = [1 2 3 4 5 6 7 8 9 10], I want to calculate the mean of SD, the two codes below gives me two answers
why bracket can make difference.
1) mean SD
2) mean (SD)
采纳的回答
更多回答(1 个)
Adam
2016-8-31
I didn't even realise
mean SD
is even valid syntax and from the example I have I can't work out what it is doing!
mean( SD )
is the proper way to call a function and that is what the brackets mean - passing the array as an argument to the mean function. What the other syntax does is a mystery!
6 个评论
Christina Chen
2016-8-31
"I can't work out what it is doing!"
Calculating the mean of the string 'SD', which is
>> +'SD'
ans =
83 68
>> mean([83,68])
ans =
75.5000
The purple color of the argument tells you that the SD is being interpreted as a string (variables are shown in black, strings in purple):

Of course the lack of brackets tells you that this is command syntax. Every beginner needs to learn about command syntax vs function syntax.
Is there any case where the command syntax
mean whatsoever
makes senses? I mean, besides computing the mean of the a ASCII values of the characters following...
If not, The MathWorks should probably throw an error and disable the command syntax here and in other cases. Gives all kinds of funny results
>> max 10
ans =
49
or
diff [10 11]
ans =
-42 -1 -16 17 0 44
Adam
2016-8-31
I've used command syntax for various things where it is documented, but I guess I never knew it was valid in other contexts like this where I would never dream of using it, but I don't work with ASCII manipulation. I suppose for those that do it is useful!
"The MathWorks should probably throw an error and disable the command syntax"
Command syntax works for all functions that I have tried: I presume that this is because it has nothing to do with the function itself, but something in the way that the MATLAB input is parsed. Most likely functions themselves don't have any way to know if they were called via function or command syntax.
As it is, this is consistent and easy to explain: command syntax is looks like this, and it always works. Why introduce inconsistencies where they are not even required? Adding inconsistencies means adding extra documentation: "sorry, even though you are an expert with a valid use-case, you cannot use command syntax with this function because a beginner once got confused..."
Beginners get confused. They learn, and the world keeps revolving.
How would a function tell the parser that it accepts command syntax or not? Keep in mind that MATLAB does not require variable declarations, so how should MATLAB even know what any particular function wants... should there be a global list of functions, or would you add something to each function? Currently MATLAB does not require lists of declarations and initializing commands at the beginning of every function, or lots of special set-up to run a function...
Should mean('SD') be prevented as well? Because someone might get confused that they can take the mean of character values? What other functions shall we prevent from working with character arrays: histc ? max ? diff ?
Why should I be prevented from calling
>> diff ThisReallyIsAString
Presumably I am not the only user who rather appreciates that in MATLAB character arrays are arrays that can be directly operated on, and that these fast numeric calculations can be performed on any character array. MATLAB is not a toy for kindergarten children, it is a research and engineering tool: why limit what researchers can do with it? Command syntax is great when looking at string encoding, for example when researching data compression or similar algorithms, to quickly display the max character value, or the min, or differences, or something similar. Very handy for sanity checking!
The nub of the question really is: how much should MATLAB protect users from themselves?
Guillaume
2016-8-31
I agree with Stephen, once command syntax is allowed, it has to be allowed for all functions. There are already too many special cases in matlab that make writing generic code a pain. I don't want to see more.
However, if it were up to me, command syntax would not be allowed at all.
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!