MATLAB: How to convert from categorical to double

categoricalconvertdoublelearningmachinemachine learning

Dear all,
I have a many different vectors 200×1 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

Best Answer

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?
Related Question