Octave model matrix -
is there simple (non loop) way create model matrix in octave. in r use model.matrix() this.
i have array:
array = [1;2;3;2]
and need (for regression reasons)
*(model = [1 0 0 0; 0 1 0 1; 0 0 1 0])* edit on side result model (colum 1 1, column 2 two's etc.: model = [1 0 0 ; 0 1 0 ; 0 0 1 ; 0 1 0]
i can loop:
model = zeros(4,3); i=1:4 model(i,array(i)) = 1; end
but nice in 1 step like:
model = model.matrix(array)
i can include in formula straight away
you need turn values linear indices so:
octave:1> array = [1 2 3 2]; octave:2> model = zeros ([numel(array) max(array)]); octave:3> model(sub2ind (size (model), 1:numel(array), array)) = 1 model = 1 0 0 0 1 0 0 0 1 0 1 0
because matrix sparse, possible optimization create sparse matrix instead.
octave:4> sp = sparse (1:numel(array), array, 1, numel (array), max (array)) sp = compressed column sparse (rows = 4, cols = 3, nnz = 4 [33%]) (1, 1) -> 1 (2, 2) -> 1 (4, 2) -> 1 (3, 3) -> 1 octave:5> full (sp) ans = 1 0 0 0 1 0 0 0 1 0 1 0
this take lot less memory many functions unable handle them , convert them full matrix anyway. whether worth dependent on want next.
Hi admin
ReplyDeletePeople suppose hacking is like bring up the keyboard and start typing like nerds and bits start falling in green on black. Shit no. Always hacking is usually taken negatively and I am positive you are trying to analyze in identical manner.
Thanks
komal