doubleConverting an array to an array of double precision numbers
double(A)A is an array of the type logical, double, complex or character.A to an array of double values.A is empty, it returns an empty array.A is not a character array, the array after conversion will have the same size as size(A).A is a character array, the size after conversion may be different from size(A), especially when A contains Unicode characters such as emojis.A is an array of complex values, it will return the same A, since complex arrays have double precision. To convert A to a real array, use real() instead.When a character array is converted to doubles using double(), each of the characters is converted to a sequence of code units. A code unit is a 16-bit integer (UTF-16) in this app.
The number of code units varies with the character being converted. For examples,
double('π') is a row vector of two code units, namely, [55357, 56985]. That's why double(['a';'π']) returns an error, since the double vectors of 'a' and 'π' have different lengths. The former has one code unit (ASCII code 97), whereas the latter has two code units (which are 55357 and 56985).
π‘ Keep this in mind. When a character array
Ais converted usingd = double(A), the sizessize(A)andsize(d)may be different. This usually happens whenAcontains emojis or non-ASCII characters.
double('abc')
ans =
97.00 98.00 99.00
double() has size ([4, 21]), which is different from size(a) (equal to [4, 5]).a = ['π»bπ¦π²βπΎ';
'π«bπ²πΆβπΏ';
'π€fπ¦π©βπΏ';
'π«dπ²π±βπΏ']
double(a)
a =
π»bπ¦π²βπΎ
π«bπ²πΆβπΏ
π€fπ¦π©βπΏ
π«dπ²π±βπΏ
ans =
Columns 1 through 7:
55357. 56827. 98.000 55356. 56806. 55356. 56818.
55357. 56491. 98.000 55356. 56818. 55356. 56822.
55357. 56740. 102.00 55356. 56806. 55356. 56809.
55356. 57323. 100.00 55356. 56818. 55356. 56817.
Columns 8 through 10:
9994.0 55356. 57342.
9994.0 55356. 57343.
9994.0 55356. 57343.
9994.0 55356. 57343.