MATLAB Answers

Why does matlab allow array indexing by a string by converting them into ascii codes?

조회 수: 12(최근 30일)
If you (by accident) enter a character array in an array indexing and it returns values not an error. It turns out that MATLAB converts characters to ASCII codes, use them as integer indices, and returns the values. For example:
X = rand(1,100);
X('A') == X(65)
I think this is just confusing, useless, and potentially risky. Does anyone know a good reason why MATLAB even supports this?

  댓글 수: 0

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume 28 Apr 2019
In many languages, and certainly in the majority of languages when matlab was created, the character type is just an alias for an integer type, and thus there was absolutely no reason for characters to be treated any different in matlab.
Modern languages do tend to treat the string (not char!) type differently and indeed matlab does too now. As dpb showed, if you use a string then you don't get that problem.
You may find it useless and confusing but for others it's useful. And certainly for somebody with a background in C-type languages, the opposite would be confusing.
In matlab, you could use this feature for caesar-type like cyphers for examples:
cypher('A':'Z') = circshift('A':'Z', 13); %build caesar cypher
cypher('THE QUICK BROWN FOX') %use cypher
The feature is used twice above: 1) to build the vector 'ABCD...Z' with the colon operator (if char wasn't treated as a number 'A':'Z' wouldn't work. 2) to index the cyper array.

  댓글 수: 2

Seung-Goo Kim
Seung-Goo Kim 28 Apr 2019
Never knew that the character type is an alias for an integer type. Then it makes a perfect sense. Thanks for a fun demo too! :D
Walter Roberson
Walter Roberson 28 Apr 2019
MATLAB can tell the difference between an integer data type and a character: ischar('A') is not the same result as ischar(65) . However, in most contexts, it converst character to double for the purpose of calculations. This is similar to logical() data type, which MATLAB can distinguish with islogical() but which is converted to numeric in most calculations.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

dpb 28 Apr 2019
편집: dpb 28 Apr 2019
Why? Because it retains consistency across data types of char() being simply an array of byes.
Indexing by alphabetical index can often be used for character translation functions if nothing else...
MATLAB being loosely typed (and initially far more loosely than presently with all the relatively recent new data types) simply leaves the decision to the programmer to use the array as wanted instead of "getting in the way" if it is the intended purpose. OTOH, if it isn't intended, yes, there is the facility to shoot foot, self.
You don't illustrate the actual code in which you discovered this feature; but one would suggest perhaps the solution would have been to have used a cellstr or string data type instead of character array.
>> X("A")
Function 'subsindex' is not defined for values of class 'string'.
>> X({'A'})
Function 'subsindex' is not defined for values of class 'cell'.
for the latter, of course, if one dereferences a cellstr() array, one ends up with a char() array so that isn't quite as bulletproof...

  댓글 수: 0

댓글을 달려면 로그인하십시오.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by