Why does converting categorical(0) to double add 1?

40 visualizaciones (últimos 30 días)
WILLIAM
WILLIAM el 27 de Nov. de 2024 a las 18:22
Editada: Stephen23 el 27 de Nov. de 2024 a las 19:29
Consider the following code:
a = categorical(0);
b = categorical(1);
disp("Categorical a")
Categorical a
disp(a)
0
disp("Double a")
Double a
disp(double(a))
1
disp("Categorical b")
Categorical b
disp(b)
1
disp("Double b")
Double b
disp(double(b))
1
Why, when converting "a" to double, does "a" become 1, when it was categorical(0) before? This behavior does not seem to happen for any other numerical value for categorical? If you have a longer categorical array such as:
c = categorical([0,1,2,3]);
then converting to double adds one to the value of each element:
disp(double(c))
1 2 3 4
Is this the intended behavior? If so, what is the motivation for it?
Thanks!

Respuestas (3)

Stephen23
Stephen23 el 27 de Nov. de 2024 a las 18:32
Editada: Stephen23 el 27 de Nov. de 2024 a las 19:29
"Why, when converting "a" to double, does "a" become 1, when it was categorical(0) before?"
It doesn't. Your comparison is not what you think it is.
" what is the motivation for it?"
It is simply because that is the first category in your (not very interesting scalar) categorical array. What value the category represents is completely irrelevant.
Do you notice the pattern?:
double(categorical(0))
ans = 1
double(categorical(1))
ans = 1
double(categorical(2))
ans = 1
double(categorical(3))
ans = 1
double(categorical(4))
ans = 1
double(categorical(5))
ans = 1
double(categorical(987654321))
ans = 1
"This behavior does not seem to happen for any other numerical value for categorical? If you have a longer categorical array such as:"
You are comparing apples with oranges. Unlike your scalar examples your non-scalar array has multiple categories (not just one), so of course converting to double will return from 1 up to the number of categories. Lets make a more interesting and illustrative example:
C = categorical([99,23,99])
C = 1x3 categorical array
99 23 99
double(C)
ans = 1×3
2 1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Note how:
  • categories based on numeric values are sorted into value order,
  • that categorical array only has two categories.
  • the values returned by DOUBLE are the indices of the categories.
"Is this the intended behavior?"
Yes. Consider sorting a categorical array: it is the desired behavior that the 1st category gets sorted before the 2nd, the 2nd category before the 3rd, the 3rd before the 4th, etc. That corresponds exactly to what DOUBLE returns: index values that will sort the categories into the correct order.
You are confusing the category index number with the value of the category. Not the same things at all.
categories(C)
ans = 2x1 cell array
{'23'} {'99'}
Note that categories are stored as text (conversion from numeric may be lossy). That is where the "value" is stored, not in the category index number (which is what you incorrectly presumed).
  1 comentario
Stephen23
Stephen23 el 27 de Nov. de 2024 a las 19:17
Editada: Stephen23 el 27 de Nov. de 2024 a las 19:22
Forget about numbers, you will only confuse yourself. Lets consider a categorical array of t-shirt sizes:
C = categorical(["L","XS","M","S","XL","M","M","S"], ["XS","S","M","L","XL"])
C = 1x8 categorical array
L XS M S XL M M S
double(C)
ans = 1×8
4 1 3 2 5 3 3 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Does this mean that L somehow gets converted to the value 4? Of course not. That is merely the index of that category in the list of categories (which are in the non-alphabetical order that I specified):
categories(C)
ans = 5x1 cell array
{'XS'} {'S' } {'M' } {'L' } {'XL'}
Now lets sort those t-shirts into size order:
S = sort(C)
S = 1x8 categorical array
XS S S M M M L XL
Again, not in alphabetic order. But they are now perfectly in order of their category indices:
double(S)
ans = 1×8
1 2 2 3 3 3 4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Which is exactly what should happen. That is rather the point of categorical arrays.

Iniciar sesión para comentar.


Matt J
Matt J el 27 de Nov. de 2024 a las 18:33
Editada: Matt J el 27 de Nov. de 2024 a las 18:35
It doesn't add 1. It just enumerates your categories (in numerical or alphabetal order), e.g.,
double( categorical(["dog","cat","fish"]) )
ans = 1×3
2 1 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
double( categorical([17,11,6,40]) )
ans = 1×4
3 2 1 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Venkat Siddarth Reddy
Venkat Siddarth Reddy el 27 de Nov. de 2024 a las 18:41
Editada: Venkat Siddarth Reddy el 27 de Nov. de 2024 a las 18:53
Hi William,
When you try to convert a categorical value to double value using "double" function, it returns the position(i.e. index) of the categorical value in the sorted categorical array.
Which means, incase of scalar arrays, irrespective of categorical value, when passed to "double" function it just returns 1 as the output:
double(categorical(123456789))
ans = 1
When a categorical array is passed to "double" function, it shows the index coresponding to their position in the sorted array:
k = [15 14 13 12];
double(categorical(k))
ans = 1×4
4 3 2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I hope this clarifies your query.
Regards
Venkat Siddarth V

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by