投票已结束
投票
Which of the following will not result in MATLAB as the output? (It may be a string or char. array.)
s = ['M','A','T','L','A','B']
9%
char([77,65,84,76,65,66])
7%
"MAT" + "LAB"
21%
upper(char('matlab' - '0' + 48))
17%
fliplr("BALTAM")
17%
rot90(rot90('BALTAM'))
30%
2929 个投票
20 个评论
The good thing is: you can just try. Even when you are writing a function or are in debugging mode, put your ideas in the Command Window and see what works.
To be honest, my approach is usually the opposite to the one I'd use here: I usually play around until I find a way that works... not until I find a way that does not work. :D
Follow-up question to the currently 27% who voted for the last option: how many of you just tried every single option? ;)
Let's analyze each option:
- s = ['M','A','T','L','A','B']
- This will result in a character array containing the letters "MATLAB". The output is MATLAB (a string).
- char([77,65,84,76,65,66])
- This will convert the ASCII values [77, 65, 84, 76, 65, 66] to characters. These ASCII values correspond to the characters 'M', 'A', 'T', 'L', 'A', 'B', respectively. So, the output will be the string "MATLAB".
- "MAT" + "LAB"
- This operation isn't valid in MATLAB. MATLAB doesn't support concatenation of strings using the "+" operator directly like this. It would result in an error. So, this option will not result in MATLAB as the output.
- upper(char('matlab' - '0' + 48))
- This expression subtracts the ASCII value of '0' from each character in the string 'matlab', adds 48, converts the resulting ASCII values to characters, and then converts them to uppercase using the upper() function. The output will be the string "MATLAB".
- fliplr("BALTAM")
- This will reverse the characters in the string "BALTAM". The output will be the string "MATLAB".
So, the correct answer is:
- "MAT" + "LAB"
I have no idea
This: fliplr("baltam") vs this: fliplr('baltam') taught me something. Thanks.
And I was not pedantic and correctly using capitals.
Is anyone willing to say out loud what the correct answer is? I tried all the selections, and they all successfully produce 'MATLAB' for me.
Rotating characters dont do what I expected. Though I shamefully admit I tried it out before voting.
Interesting. The correct answer was not what I had expected. Time to dig in the doc to see why this is the case...