5

I am curious about what is the best way to do this: assume I have a 10x10 zero matrix and I want to replace the zeros with ones with the known coordinates, in the beginning I am thing about write a for loop to replace the elements one by one by reading out the x and y. Is there any other easier way to do it?

Example:

mat=zeros(10);
x=[1,3,5,7,9]'; 
y=[2,4,6,8,10]';
newmat= [0 0 0 0 0 0 0 0 0 0
         1 0 0 0 0 0 0 0 0 0 
         0 0 0 0 0 0 0 0 0 0
         0 0 1 0 0 0 0 0 0 0
         0 0 0 0 0 0 0 0 0 0
         0 0 0 0 1 0 0 0 0 0
         0 0 0 0 0 0 0 0 0 0 
         0 0 0 0 0 0 1 0 0 0
         0 0 0 0 0 0 0 0 0 0
         0 0 0 0 0 0 0 0 1 0]  
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
tytamu
  • 555
  • 3
  • 11
  • 20
  • I just realized that this is a duplicate of [a recent question](http://stackoverflow.com/questions/5886039/matlab-addressing-of-one-index-without-sub2ind/5889492#5889492), which I answered. However, I do not blame you for not being able to find it as the title of the other question would not have helped you in searching. – abcd May 08 '11 at 03:11
  • @Yoda: I think my post is somewhat confusing, I did not intend to have eye shape assignment but just substitute the elements according to random x and y. But thanks for the help :) – tytamu May 09 '11 at 21:52
  • Yen: No problem. @eat's solution is more general, and had your example been different, that is exactly what I would've suggested. – abcd May 09 '11 at 21:57

2 Answers2

5

For this kind of manipulations use sub2ind, like

> mat=zeros(10); x=[1,3,5,7,9]'; y=[2,4,6,8,10]';
> mat(sub2ind([10 10], y, x))= 1
mat =
   0   0   0   0   0   0   0   0   0   0
   1   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   1   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   1   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   1   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   1   0

Update: To contrast this with innocent looking assigmnet mat(y, x)= 1.

> mat= zeros(10);
> mat(y, x)= 1
mat =
   0   0   0   0   0   0   0   0   0   0
   1   0   1   0   1   0   1   0   1   0
   0   0   0   0   0   0   0   0   0   0
   1   0   1   0   1   0   1   0   1   0
   0   0   0   0   0   0   0   0   0   0
   1   0   1   0   1   0   1   0   1   0
   0   0   0   0   0   0   0   0   0   0
   1   0   1   0   1   0   1   0   1   0
   0   0   0   0   0   0   0   0   0   0
   1   0   1   0   1   0   1   0   1   0
eat
  • 7,440
  • 1
  • 19
  • 27
3

You can do what you want by indexing the specific rows and columns into the matrix and assigning values to the diagonal.

mat(y,x)=eye(length(x))
mat =

     0     0     0     0     0     0     0     0     0     0
     1     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0
     0     0     1     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     1     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     1     0     0     0
     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     1     0
abcd
  • 41,765
  • 7
  • 81
  • 98