Highlights
Follow


Poll is CLOSED

Poll

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 votes

Harald
Harald on 15 Mar 2024
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? ;)
Dr.GADDALA JAYA RAJU
Dr.GADDALA JAYA RAJU on 13 Mar 2024
Let's analyze each option:
  1. s = ['M','A','T','L','A','B']
  • This will result in a character array containing the letters "MATLAB". The output is MATLAB (a string).
  1. 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".
  1. "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.
  1. 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".
  1. 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"
Stephen23
Stephen23 on 19 Mar 2024 (Edited on 19 Mar 2024)
"Let's analyze each option"
An authoritative sounding comment... lets check some of the statements:
  • "s = ['M','A','T','L','A','B'].... "MATLAB". The output is MATLAB (a string)." The output is not a string, the output is a character vector.
  • "char([77,65,84,76,65,66]) ... So, the output will be the string "MATLAB"." The output is not a string, the output is a character vector.
  • ""MAT" + "LAB" This operation isn't valid in MATLAB. MATLAB doesn't support concatenation of strings using the "+" operator directly like this." Actually MATLAB does support the PLUS operator on string arrays.
  • "It would result in an error. So, this option will not result in MATLAB as the output." You, the reader, can easily test this yourself: does it throw an error? (hint: the syntax has been valid since R2017a)
  • "upper(char('matlab' - '0' + 48)) ... This expression subtracts the ASCII value of '0' from each character in the string 'matlab' .... The output will be the string "MATLAB"." They are not string arrays, they are both character vectors.
  • "fliplr("BALTAM") This will reverse the characters in the string "BALTAM". In this case the output will be a string "MATLAB"." For a change the output will actually be a string array... but consider what happens when you flip a scalar array along any dimension: you get exactly the same scalar array. Now consider that "BALTAM" is a scalar string array. Of course you can also test it yourself in MATLAB.
A lack of understanding about MATLAB (especially understanding the differences between string arrays and character vectors) leads to many incorrect statements about what MATLAB operations will do:
A lack of testing does not help either.
Image Analyst
Image Analyst on 27 Mar 2024
Yes, technically strings and character arrays are now different but before MATLAB introduced strings, many people called character arrays "strings", just as in other programming languages. Though one should now be more precise in using the word "string" there are still many, even advanced MATLAB programmers, who use the word "string", perhaps sloppily but that's the habit they've grown up with. For example if you do
str = 'abc';
and ask people what str is, many will say/write string even though they know it's actually a character vector and not a string object. But I agree with your post that it's best to try to be accurate with this ambiguous situation we're now in.
Chris
Chris on 24 Mar 2024 (Edited on 24 Mar 2024)
It looks to me like he's posting unfiltered ChatGPT answers. Indicators include, but are not limited to:
  • Bulleted lists
1. Numbered lists consisting of one element (copied in PowerPoint?).
1. Unwarranted initial enthusiasm (not here, but elsewhere).
1. Voluntary, long-winded examples where none were requested.
Keep in mind that as AI technology evolves, these models become increasingly sophisticated, making it harder to tell AI-generated content from human-created text. AI models such as ChatGPT are designed to produce human-like text based on their training data, striving for accuracy, coherence, and relevance, which can blur the lines even further.
DGM
DGM on 27 Mar 2024
This user has been making a name in regards to pasting unformatted, unverified, and often irrelevant trash directly from ChatGPT, at times even being so careless as to copy-paste his own prompts and other text from the interface.
Dyuman Joshi
Dyuman Joshi on 26 Mar 2024
That response was definitely copy-pasted from some AI (albeit quite an awful one), as can be observed in the different answers posted (on MATLAB Answers) by the same user as well.
However, your response, in itself, is also an AI generated one :- Bullet point on the point of bulleting itself, numbered list with the same numbers.
Might be ironical but who knows (I don't, lol), ¯\_(ツ)_/¯.
Chris
Chris on 27 Mar 2024
I don't think my answer was quite as formulaic as an LLM answer, but yes I suppose I was being ironical. The last bit did come from a ChatGPT response.
goc3
goc3 on 13 Mar 2024
I appreciate the analysis approach here, but it does have a few problems.
The first two options result in a character array (which uses single quotes) rather than a string (which uses double quotes).
A character array and string are different data types in MATLAB. While they can be used interchangeably in certain contexts, they are NOT the same and do NOT always behave in the same ways.
The + operator DOES WORK for string concatenation.
Certain functions (hint: see some of the remaining options) treat character arrays and strings differently.
達文
達文 on 12 Mar 2024
I have no idea
Mike Susedik
Mike Susedik on 8 Mar 2024
This: fliplr("baltam") vs this: fliplr('baltam') taught me something. Thanks.
And I was not pedantic and correctly using capitals.
Matt J
Matt J on 6 Mar 2024
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.
Dyuman Joshi
Dyuman Joshi on 6 Mar 2024 (Edited on 26 Mar 2024)
I suspect you might have used a char array instead of a string scalar for the fliplr() case.
Using fliplr() flips the order of string scalar elements; it does not change the text inside each string scalar element (which is the case for a char array).
fliplr(["MATLAB" "ABCD"])
would produce
["ABCD" "MATLAB"]
Using fliplr() on a string array is same as using it on a cell array.
(For comparison, using fliplr() on a char array is similar to using it on a numerical array)
Rik
Rik on 7 Mar 2024
I try to always treat a string as a cellstr with generous helpings of syntactic sugar. I guess I forgot in this case.
I had expected fliplr to be smart(?) enough to do string(cellfun(@fliplr,cellstr(A),'uni',0)). So, on reflection, I agree with the current behavior.
Christopher Stapels
Christopher Stapels on 5 Mar 2024
Rotating characters dont do what I expected. Though I shamefully admit I tried it out before voting.
Chen Lin
Chen Lin on 5 Mar 2024
Same here. I used MATLAB Online.
Rik
Rik on 5 Mar 2024
Interesting. The correct answer was not what I had expected. Time to dig in the doc to see why this is the case...
Chen Lin
Chen Lin on 5 Mar 2024
I did spend some time digging into the doc and found out why.
the cyclist
the cyclist on 4 Mar 2024
@goc3, you are a person of deceptive character, stringing us along like this.
goc3
goc3 on 4 Mar 2024
@the cyclist: well said.

See Also

Tags

No tags entered yet.