"char" doesn't always maintain orientation of input array

2 次查看(过去 30 天)
The "char" function maintains orientation of input arrays except for categorical arrays:
>> categorical(["A" "B"])
ans = 1×2 categorical array
A B
>> char(categorical(["A" "B"]))
ans = 2×1 char array
'A'
'B'
Does anyone know the rationale behind this?

回答(1 个)

Voss
Voss 2022-11-8
I imagine the rationale is that, in case the elements of the categorical array are multiple characters long, e.g.:
categorical(["Abbot" "Beto"])
ans = 1×2 categorical array
Abbot Beto
You want to be able to know where one ends and the next one begins, so you put them on different rows:
char(categorical(["Abbot" "Beto"]))
ans = 2×5 char array
'Abbot' 'Beto '
as opposed to having the result be:
'AbbotBeto'
ans = 'AbbotBeto'
which can be ambiguous:
categorical(["0" "1" "10" "11"])
ans = 1×4 categorical array
0 1 10 11
char(categorical(["0" "1" "10" "11"]))
ans = 4×2 char array
'0 ' '1 ' '10' '11'
since in this case '011011' could be interpreted as '0', '1', '10', '11', or as '0', '11', '0', '11', for example.
Just a guess.
  1 个评论
FM
FM 2022-11-9
编辑:FM 2022-11-9
Thanks for the speculation, Voss. I see this as trading one kind of ambiguity for another. The new ambiguity is that you can't tell whether the original array was a row or column vector.
It's odd that char doesn't use the same approach for string and categorical arrays:
>> char(["dog" "catty"])
1×5×2 char array
ans(:,:,1) = 'dog '
ans(:,:,2) = 'catty'
>> char(categorical(["dog" "catty"]))
ans = 2×5 char array
'dog '
'catty'
In fact, the categorical array is treated the same as a cell array of char arrays:
>> char({'dog';'catty'})
ans = 2×5 char array
'dog '
'catty'

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by