Tina T
Tina T

Reputation: 205

Assign an entire row to multiple rows of another matrix in matlab

I have a 13 by 3 matrix called face.

face =

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

And I have an array newset1, which contains the indices of the rows of the matrix 'face' which have to be assigned a new value.

newset1 = [5,1,7]

The "new value" is a vector shown below

value = [7,8,9]

I know how to access the rows whose values should be updated. Like this : face(newset1,:)

ans =

     1     1     1
     1     1     1
     1     0     0

And I want to do something like this

face(newset1,:) = value

And have my output look like this :

face =

     7     8     9
     1     1     1
     1     0     0
     1     1     1
     7     8     9
     1     0     0
     7     8     9
     1     1     1
     1     1     1
     1     1     1
     1     1     1
     0     0     0
     0     0     0

But I get the following error.

Subscripted assignment dimension mismatch.

It makes sense to me, what I'm doing, but since it doesn't work, I am pretty sure that I'm wrong. I'd also prefer not to use a for-loop, because I've read that matlab slows down on loops.

Upvotes: 2

Views: 1563

Answers (1)

Naisheel Verdhan
Naisheel Verdhan

Reputation: 5133

Try using repmat() function. See here: MATLAB: duplicating vector 'n' times.

Your desired output is achievable by

face(newset1,:)=repmat(value,length(newset1),1)

Upvotes: 3

Related Questions