-1

I have the following matrix:

a =

     1     4
     6     4

After that, I created a zero matrix new of the same size of a:

new =

     0     0
     0     0 

I'm trying to do the following:

  • assign the location (2,1) in a to the variable p
  • for that location in new, I want to give it the value 1

For that, I did the following:

p=a(2,1);
new(p)=1;

But, I got the following:

In an assignment  A(I) = B, a matrix A cannot be resized.

It seems that also p=a(2,1); assigns the value at that location and not the location itself.

How can I solve this issue?

Thanks.

Simplicity
  • 47,404
  • 98
  • 256
  • 385

1 Answers1

1

If you want the "location" you can use the linear index of that pixel:

p = sub2ind( size(a), 2, 1 );
new(p) = 1;
Shai
  • 111,146
  • 38
  • 238
  • 371
  • 1
    I would give you a +1 Shai, but I sincerely hope that @Simplicity stops getting answers in the MATLAB tag. I know you have been annoyed at the same user earlier. Answering doesn't help. [Here](http://stackoverflow.com/questions/20677692/saving-a-labeled-image), [here](http://stackoverflow.com/questions/20679066/i-have-labeled-regions-but-where-are-they-on-the-image). And you're not the only one: [thewaywewalk](http://stackoverflow.com/questions/20680251/returning-specific-regions), and you again, [here](http://stackoverflow.com/questions/20682446/finding-pixel-locations). There are more... – Stewie Griffin Feb 23 '14 at 18:07