2

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.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
Tina T
  • 135
  • 8

1 Answers1

3

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

Your desired output is achievable by

face(newset1,:)=repmat(value,length(newset1),1)
Community
  • 1
  • 1
Naisheel Verdhan
  • 4,905
  • 22
  • 34
  • Thanks a bunch. It works perfectly. And I looked it up right now. I'm guessing that it basically replicates the row value like this >> repmat(value,length(newset1),1) ans = 7 8 9 7 8 9 7 8 9 And then it assigns it to the rows I need it. Once again, thanks!! – Tina T Apr 29 '14 at 05:21
  • And the repmat function does not use loops, so you won't slow down. – Naisheel Verdhan Apr 29 '14 at 05:25
  • That's good news :) I've become so used to using loops that my matlab program takes forever to run. So now I'm in the process of rewriting everything, getting rid of as many loops I can. Once again, Thanks :) – Tina T Apr 29 '14 at 05:29
  • @TinaMaria Please accept the answer if it anwsered your question. That way other people will know this question have a good answer. – patrik Apr 29 '14 at 06:15
  • @TinaMaria Few more ways to replicate data are described [here](http://stackoverflow.com/questions/22847086/how-to-replicate-an-array) – Divakar Apr 29 '14 at 07:38