Suppose I have a matrix such that each row is a standard basis vector, i.e. each row contains exactly one 1, the other columns being 0.
Is there a convenient way to create such a matrix (i.e. given a vector of positions of where the ones are in each row)?
Also, is there a way I should represent such a matrix so that multiplications with it can be done more efficiently in octave?
Suppose you want a 3×3 matrix with the ones in columns 3, 1, and 2 respectively:
will give you a matrix with 9 elements, most zero, with the ones in the desired places. You can save memory by using a sparse representation:
sparse_x = sparse(x);. But the following test on my machine implies that the natural form multiplies faster:This was Octave 3.4 on a Core i7, YMMV.
Looking at
whosit appears that Octave is doing something clever withx:If it knows
xis special, maybe it’s already taking advantage of speedups in the multiplication.