1-octave coding

I am trying to get binary single-axis encoding of an integer vector in Octave. I have a vector y, let's say

y = [1 ; 2 ; 3 ; 1 ; 3]

and i need a matrix

Y = [1 0 0
     0 1 0
     0 0 1
     1 0 0
     0 0 1]

I can build matrix 1 from K manually using

Y = [];
Y = [Y y == 1];
Y = [Y y == 2];
Y = [Y y == 3];

But when I try to build it with a loop for,

Y = [];
for i = unique(y),
    Y = [Y y == i];
endfor

something will go wrong:

error: mx_el_eq: nonconformant arguments (op1 is 5x1, op2 is 3x1)

I don’t even understand the error message. Where is my mistake?

+4
source share
2 answers

Ok, found it. I want the textbook to tell me this.

Y = [];
for i = unique(y)',
%                ^
%  -------------/
    Y = [Y y == i];
end

-, for unique -, " " y (5 Γ— 1) unique(y) (3 Γ— 1).

+2

, :

Y = unique(y)(:,ones(1,size(y,1)))' == y(:,ones(size(unique(y),1),1))
+3

All Articles