Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
set value but squares appear!!!

Subject: set value but squares appear!!!

From: Dongsheng

Date: 30 Mar, 2011 15:09:05

Message: 1 of 6

Hi, everyone

I met a strange question. I want set some pixels in my matrix to 1. I have gotten this address in my variable co. co is a two column variable, one corresponds to x address and the other corresponds to y address. My scripts are

I=zeros (512);
I(co(:,1),co(:,2))=1;

Sounds simple? No, If you use this script you will see some squares appear in I instead of what I want. But this method seems working after you use 'find' script. Why? Is this a bug of Matlab?

All the best
Dongsheng

Subject: set value but squares appear!!!

From: ImageAnalyst

Date: 30 Mar, 2011 16:58:21

Message: 2 of 6

Dongsheng  :
For every row that's in there, it does all the columns, and vice
versa. That's just the way it works.
Try this to get it to do what you want:

co = int32(randi(512, [1000 2]))
I=zeros (512);
rows = co(:,1);
cols = co(:,2);
for k = 1 : size(co, 1)
       % Set each point one at a time.
I(rows(k), cols(k))=1;
end
imshow(I);

Subject: set value but squares appear!!!

From: Steven_Lord

Date: 30 Mar, 2011 17:10:26

Message: 3 of 6



"Dongsheng " <dxh915@bham.ac.uk> wrote in message
news:imvh2h$cp9$1@fred.mathworks.com...
> Hi, everyone
>
> I met a strange question. I want set some pixels in my matrix to 1. I have
> gotten this address in my variable co. co is a two column variable, one
> corresponds to x address and the other corresponds to y address. My
> scripts are
>
> I=zeros (512);
> I(co(:,1),co(:,2))=1;
>
> Sounds simple? No, If you use this script you will see some squares appear
> in I instead of what I want. But this method seems working after you use
> 'find' script. Why? Is this a bug of Matlab?

This is NOT a bug. When you index into an array with two subscripts and at
least one is nonscalar, MATLAB will take all combinations of elements from
each subscript. So for example this:

A = magic(4);
A([2 3], [1 4]) = -1;

will set elements (2, 1) and (3, 4) of A to -1, but it will also set
elements (2, 4) and (3, 1) to -1 as well.

To set just those elements whose coordinates are given by corresponding
elements of your indices, use SUB2IND to convert the subscripts to linear
indices.

http://www.mathworks.com/help/techdoc/math/f1-85462.html#f1-85511

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: set value but squares appear!!!

From: Dongsheng

Date: 30 Mar, 2011 22:25:20

Message: 4 of 6

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <4c0b7c41-2f6f-42ef-978b-3fcf8259fe46@r4g2000vbq.googlegroups.com>...
> Dongsheng  :
> For every row that's in there, it does all the columns, and vice
> versa. That's just the way it works.
> Try this to get it to do what you want:
>
> co = int32(randi(512, [1000 2]))
> I=zeros (512);
> rows = co(:,1);
> cols = co(:,2);
> for k = 1 : size(co, 1)
> % Set each point one at a time.
> I(rows(k), cols(k))=1;
> end
> imshow(I);

Thanks. but this is not the purpose of Matlab which is good at matrix instead of loops.

Subject: set value but squares appear!!!

From: Dongsheng

Date: 30 Mar, 2011 22:27:05

Message: 5 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message <imvo4f$gr5$1@fred.mathworks.com>...
>
>
> "Dongsheng " <dxh915@bham.ac.uk> wrote in message
> news:imvh2h$cp9$1@fred.mathworks.com...
> > Hi, everyone
> >
> > I met a strange question. I want set some pixels in my matrix to 1. I have
> > gotten this address in my variable co. co is a two column variable, one
> > corresponds to x address and the other corresponds to y address. My
> > scripts are
> >
> > I=zeros (512);
> > I(co(:,1),co(:,2))=1;
> >
> > Sounds simple? No, If you use this script you will see some squares appear
> > in I instead of what I want. But this method seems working after you use
> > 'find' script. Why? Is this a bug of Matlab?
>
> This is NOT a bug. When you index into an array with two subscripts and at
> least one is nonscalar, MATLAB will take all combinations of elements from
> each subscript. So for example this:
>
> A = magic(4);
> A([2 3], [1 4]) = -1;
>
> will set elements (2, 1) and (3, 4) of A to -1, but it will also set
> elements (2, 4) and (3, 1) to -1 as well.
>
> To set just those elements whose coordinates are given by corresponding
> elements of your indices, use SUB2IND to convert the subscripts to linear
> indices.
>
> http://www.mathworks.com/help/techdoc/math/f1-85462.html#f1-85511
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Thanks. It is working!!! But still cannot figure out why sometimes it works after I use find function.

Subject: set value but squares appear!!!

From: ImageAnalyst

Date: 31 Mar, 2011 01:57:31

Message: 6 of 6

On Mar 30, 6:25 pm, "Dongsheng " <dxh...@bham.ac.uk> wrote:
> Thanks. but this is not the purpose of Matlab which is good at matrix instead of loops.
---------------------------------------------------------------------------------------------
Dongsheng :
Oh really!?! Is that so, or is that *always* so? How do you think
sub2ind() works? Do you think it might internally use a for loop to
convert a long list of coordinates into linear indices? How does it
know to move from one coordinate to the next?

I guess you weren't here when we were having a discussion about for
loops versus other methods and how for loops are often unfairly
maligned. So a little demo/tutorial is now called for. We found
cases where the for loop is much faster. In fact, believe it or not,
your case is one of those. Here, try this code and you'll see that
the for loop is 2-12 times FASTER. On my computer the for loop
averaged 5 times faster.
Here's some sample output:
    The time for the "for" loop is 0.000103
    The time using sub2ind is 0.000730, a factor of 7.103261 longer

Here's the code:

% Demo to prove to Dongsheng that for loops can be faster.
% Initialize an array.
myArray = zeros (512);

% Generate a list of 1000 random coordinates
% that we will assign a value of 1 to.
co = int32(randi(512, [1000 2]));
rows = co(:,1);
cols = co(:,2);

% First assign values using for loop.
% Start timer
tStart1 = tic;
for k = 1 : size(co, 1)
       % Set each point one at a time.
        myArray(rows(k), cols(k))=1;
end
tElapsed1 = toc(tStart1);
fprintf('The time for the "for" loop is %f\n', tElapsed1);
% Note the short time.

% Now do again, this time timing the assignment where we
% first get linear indexes using sub2ind,
% instead of using a for loop.
tStart2 = tic;
linearIndexes = sub2ind([512 512], rows, cols);
myArray(linearIndexes) = 1;
tElapsed2 = toc(tStart2);
fprintf('The time using sub2ind is %f, ', tElapsed2);
fprintf('a factor of %f longer\n', tElapsed2/tElapsed1);
% Note the time of sub2ind is about 2-12 times as long
% as it was using the for loop.


For your case it doesn't really matter - both are faster than a rocket
powered cheetah on steroids, but if time really matters it could be
worth trying it both ways.
ImageAnalyst

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us