- 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.
Why does converting categorical(0) to double add 1?
조회 수: 43 (최근 30일)
이전 댓글 표시
Consider the following code:
a = categorical(0);
b = categorical(1);
disp("Categorical a")
disp(a)
disp("Double a")
disp(double(a))
disp("Categorical b")
disp(b)
disp("Double b")
disp(double(b))
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))
Is this the intended behavior? If so, what is the motivation for it?
Thanks!
댓글 수: 0
답변 (3개)
Stephen23
2024년 11월 27일 18:32
편집: Stephen23
2024년 11월 27일 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))
double(categorical(1))
double(categorical(2))
double(categorical(3))
double(categorical(4))
double(categorical(5))
double(categorical(987654321))
"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])
double(C)
Note how:
"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)
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
Stephen23
2024년 11월 27일 19:17
편집: Stephen23
2024년 11월 27일 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"])
double(C)
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)
Now lets sort those t-shirts into size order:
S = sort(C)
Again, not in alphabetic order. But they are now perfectly in order of their category indices:
double(S)
Which is exactly what should happen. That is rather the point of categorical arrays.
Venkat Siddarth Reddy
2024년 11월 27일 18:41
편집: Venkat Siddarth Reddy
2024년 11월 27일 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))
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))
I hope this clarifies your query.
Regards
Venkat Siddarth V
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!