# Representación de Matlab / Octave 1 de K

I have a y of size 5000,1 (matrix), which contains integers between 1 and 10. I want to expand those indices into a 1-of-10 vector. I.e., y contains 1,2,3... and I want it to "expand" to:

``````1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
``````

¿Cuál es la mejor forma de hacerlo?

Lo intenté:

``````Y = zeros(5000,10); Y(y) = 1;
``````

pero no funcionó.

It works for vectors though:

if `y = [2 5 7]`y `Y = zeros(1,10)`, entonces `Y(y) = [0 1 0 0 1 0 1 0 0 0]`.

preguntado el 08 de noviembre de 11 a las 17:11

## 5 Respuestas

Considere lo siguiente:

``````y = randi([1 10],[5 1]);       %# vector of 5 numbers in the range [1,10]
yy = bsxfun(@eq, y, 1:10)';    %# 1-of-10 encoding
``````

Ejemplo:

``````>> y'
ans =
8     8     4     7     2
>> yy
yy =
0     0     0     0     0
0     0     0     0     1
0     0     0     0     0
0     0     1     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     1     0
1     1     0     0     0
0     0     0     0     0
0     0     0     0     0
``````

respondido 08 nov., 11:21

That's what I'm looking for, give or take a transpose :-) Thanks! - Frank

@Frank: I also believe the Statistics Toolbox has a somewhat similar function DUMMYVAR - Amro

``````n=5
Y = ceil(10*rand(n,1))
Yexp = zeros(n,10);
Yexp(sub2ind(size(Yexp),1:n,Y')) = 1
``````

Also, consider using sparse, as in: Creación de matriz de indicadores.

contestado el 23 de mayo de 17 a las 13:05

Are there relative merits/demerits to bsxfun v. sub2ind? - Frank

@Frank: this is indexing-based, mine involves making equality-comparisons... Would be interesting to see how they compare in terms of performance - Amro

I checked the performance. The sub2ind solution is slightly faster than the bsxfun solution. - cyborg

While sparse may be faster and save memory, an answer involving eye() would be more elegant as it is faster than a loop and it was introduced during the octave lecture of that class

Here is an example for 1 to 4

``````V = [3;2;1;4];
I = eye(4);
Vk = I(V, :);
``````

respondido 26 mar '12, 06:03

What works for me is eye(K)(V,:) which is similar to yours but condenses lines 2 and 3 into one line. More usefully though, K is number of possible classes, not actually present classes, so for example if there were 10 possible outcomes of which 4 actually were present, it would work more correctly and more reliably. - Geoffrey Anderson

You can try cellfun operations:

``````function vector = onehot(vector,decimal)
vector(decimal)=1;
end
aa=zeros(10,2);
dec=[5,6];
%split into columns
C=num2cell(aa,1);
D=num2cell(dec,1);
onehotmat=cellfun("onehot",C,D,"UniformOutput",false);
output=cell2mat(onehotmat);
``````

contestado el 29 de mayo de 17 a las 02:05

Creo que quieres decir:

``````y = [2 5 7];
Y = zeros(5000,10);
Y(:,y) = 1;
``````

After the question edit, it should be this instead:

``````y = [2,5,7,9,1,4,5,7,8,9....]; //(size (1,5000))
for i = 1:5000
Y(i,y(i)) = 1;
end
``````

respondido 08 nov., 11:21

This will set the entire y columns to 1. - cyborg

I wanted to avoid writing the loop, which is the most obvious solution :-) - Frank

question wasn't very clear until Amro edited it :), be sure to accept the other guy's answer - Smash

His question literally is what the best way of doing it is, and you pose a for loop XD. - TimZaman

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.