Nik O'Lai
Nik O'Lai

Reputation: 3694

Setting values in a matrix in bulk

The question is about bulk-changing values in a matrix based on data contained in a vector.

Suppose I have a matrix 5x4 matrix of zeroes.

octave> Z = zeros(5,4)

Z =

   0   0   0   0
   0   0   0   0
   0   0   0   0
   0   0   0   0
   0   0   0   0

And a column vector of length equal to the number of rows in Z, that is, 5. The rows in the vector y correspond to rows in the matrix Z.

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

y =

   1
   3
   2
   1
   3

What I want is to set 1's in the matrix Z in the columns whose indices are contained as values in the corresponding row of the vector y. Namely, I'd like to have Z matrix like this:

Z =                       #  y =

   1   0   0   0          # <--  1 st column
   0   0   1   0          # <--  3 rd column
   0   1   0   0          # <--  2 nd column
   1   0   0   0          # <--  1 st column
   0   0   1   0          # <--  3 rd column

Is there a concise way of doing it? I know I can implement it using a loop over y, but I have a feeling Octave could have a more laconic way. I am new to Octave.

Upvotes: 0

Views: 186

Answers (2)

Nik O&#39;Lai
Nik O&#39;Lai

Reputation: 3694

Found another solution that does not use broadcasting. It does not need a matrix of zeroes either.

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

octave> eye(5)(y,:)
ans =

   1   0   0   0   0
   0   0   1   0   0
   0   1   0   0   0
   1   0   0   0   0
   0   0   1   0   0

Relevant reading here: http://www.gnu.org/software/octave/doc/interpreter/Creating-Permutation-Matrices.html

Upvotes: 2

carandraug
carandraug

Reputation: 13091

Since Octave has automatic broadcasting (you'll need Octave 3.6.0 or later), the easies way I can think is to use this with a comparison. Here's how

octave> 1:5 == [1 3 2 1 3]'
ans =

   1   0   0   0   0
   0   0   1   0   0
   0   1   0   0   0
   1   0   0   0   0
   0   0   1   0   0

Broadcasting is explained on the Octave manual but Scipy also has a good explanation for it with nice pictures.

Upvotes: 2

Related Questions