How to convert from categorical to double ?

86 views (last 30 days)
Dear all,
I have a many different vectors 200x1 of categorical 0.5 0.75 1 1.25 1.5. These are the results of my machine learning predictions.
I simply want to reconvert these into double to make some calculations, i.e. averages of the prediction etc etc.
Many thanks in advance for helping me with that,
Pierre
  1 Comment
Pierre Lonfat
Pierre Lonfat on 13 Apr 2018
Edited: Pierre Lonfat on 13 Apr 2018
For anyone having the same trouble here is my (slow code) solution:
CATEGORIES_MATCHING_MATRIX=[YOUR_CATEGORIES_IN_DOUBLE; double(unique(CATEGORIES_MATRIX_RESULTS)')];
for i=1:length(CATEGORIES_MATRIX_RESULTS);
for j=1:size(CATEGORIES_MATRIX_RESULTS,2)
CATEGORIES_MATRIX_RESULTS(i,j)=CATEGORIES_MATCHING_MATRIX(1,CATEGORIES_MATRIX_RESULTS(i,j));
end
end

Sign in to comment.

Accepted Answer

Peter Perkins
Peter Perkins on 13 Apr 2018
Presumably when you created your categorical arrays, you used the vector [0.5 0.75 1 1.25 1.5] to define the unique raw values. Once you convert to categorical, they are GONE - calling categorical is a data conversion. You can convert back to double using the double function, but as you have observed, what you get are the category numbers.
But the best way to get back to your original numeric values is to save that 1x5 vector, and subscript into it using your categorical array. That's all you need to do. No loops, just one line.
>> x = [1 2 3]
x =
1 2 3
>> c = categorical([1 2 3 2 1],x,{'a' 'b' 'c'})
c =
1×5 categorical array
a b c b a
>> x(c)
ans =
1 2 3 2 1
However: one might ask the question, if your categories have "numeric" names, and you need to use the corresponding values to do numeric computations, why use categorical at all?
  1 Comment
Pierre Lonfat
Pierre Lonfat on 15 Apr 2018
Thank you very much Peter ! I used categorical for some machine learning issues. I wanted to be sure that my classification methods recognise my output variable as categorical, this is why I converted it at the beginning :) !
Thank you for your answer !

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!