I am looking for an efficient, vectorised way to expand a column vector of category numbers into a matrix of normalised features.
For example, I might have:
octave:16> false_vals
false_vals =
-0.10000 -0.20000 -0.50000
octave:17> true_vals
true_vals =
0.90000 0.80000 0.50000
octave:18> cats
cats =
1
2
3
And I would like to combine them to create a matrix where each row was the same as false_vals
but with the index position identified by cats
set to the corresponding value from true_vals
. For the example, that would look like this:
octave:19> X
X =
0.90000 -0.20000 -0.50000
-0.10000 0.80000 -0.50000
-0.10000 -0.20000 0.50000
I found this question and answer that I can extend to give me a random allocation:
octave:28> X = repmat(false_vals,3,1)
X =
-0.10000 -0.20000 -0.50000
-0.10000 -0.20000 -0.50000
-0.10000 -0.20000 -0.50000
octave:29> Y = repmat(true_vals,3,1)
Y =
0.90000 0.80000 0.50000
0.90000 0.80000 0.50000
0.90000 0.80000 0.50000
octave:30> L = ( rand(3,3) > 0.5 ) % This stands in for the actual logical matrix I want
L =
1 1 1
1 1 1
0 0 1
octave:31> X(L) = Y(L)
X =
0.90000 0.80000 0.50000
0.90000 0.80000 0.50000
-0.10000 -0.20000 0.50000
But I am then stuck on how to replace the rand
above with some function that can turn my cats
vector into
L =
1 0 0
0 1 0
0 0 1
I.e. I know I can do what I want with a logical matrix, but don't know how to get from my vector cats
to the correct logical matrix (for each row of L
, column position identified by value of that row in cats
vector should be true, so I can use it in the last statement).