http://www.mathworks.com/matlabcentral/newsreader/view_thread/305371
MATLAB Central Newsreader  set value but squares appear!!!
Feed for thread: set value but squares appear!!!
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 30 Mar 2011 15:09:05 +0000
set value but squares appear!!!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305371#828348
Dongsheng
Hi, everyone<br>
<br>
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<br>
<br>
I=zeros (512);<br>
I(co(:,1),co(:,2))=1;<br>
<br>
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?<br>
<br>
All the best<br>
Dongsheng

Wed, 30 Mar 2011 16:58:21 +0000
Re: set value but squares appear!!!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305371#828358
ImageAnalyst
Dongsheng :<br>
For every row that's in there, it does all the columns, and vice<br>
versa. That's just the way it works.<br>
Try this to get it to do what you want:<br>
<br>
co = int32(randi(512, [1000 2]))<br>
I=zeros (512);<br>
rows = co(:,1);<br>
cols = co(:,2);<br>
for k = 1 : size(co, 1)<br>
% Set each point one at a time.<br>
I(rows(k), cols(k))=1;<br>
end<br>
imshow(I);

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

Wed, 30 Mar 2011 22:25:20 +0000
Re: set value but squares appear!!!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305371#828428
Dongsheng
ImageAnalyst <imageanalyst@mailinator.com> wrote in message <4c0b7c412f6f42ef978b3fcf8259fe46@r4g2000vbq.googlegroups.com>...<br>
> Dongsheng :<br>
> For every row that's in there, it does all the columns, and vice<br>
> versa. That's just the way it works.<br>
> Try this to get it to do what you want:<br>
> <br>
> co = int32(randi(512, [1000 2]))<br>
> I=zeros (512);<br>
> rows = co(:,1);<br>
> cols = co(:,2);<br>
> for k = 1 : size(co, 1)<br>
> % Set each point one at a time.<br>
> I(rows(k), cols(k))=1;<br>
> end<br>
> imshow(I);<br>
<br>
Thanks. but this is not the purpose of Matlab which is good at matrix instead of loops.

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

Thu, 31 Mar 2011 01:57:31 +0000
Re: set value but squares appear!!!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305371#828458
ImageAnalyst
On Mar 30, 6:25 pm, "Dongsheng " <dxh...@bham.ac.uk> wrote:<br>
> Thanks. but this is not the purpose of Matlab which is good at matrix instead of loops.<br>
<br>
Dongsheng :<br>
Oh really!?! Is that so, or is that *always* so? How do you think<br>
sub2ind() works? Do you think it might internally use a for loop to<br>
convert a long list of coordinates into linear indices? How does it<br>
know to move from one coordinate to the next?<br>
<br>
I guess you weren't here when we were having a discussion about for<br>
loops versus other methods and how for loops are often unfairly<br>
maligned. So a little demo/tutorial is now called for. We found<br>
cases where the for loop is much faster. In fact, believe it or not,<br>
your case is one of those. Here, try this code and you'll see that<br>
the for loop is 212 times FASTER. On my computer the for loop<br>
averaged 5 times faster.<br>
Here's some sample output:<br>
The time for the "for" loop is 0.000103<br>
The time using sub2ind is 0.000730, a factor of 7.103261 longer<br>
<br>
Here's the code:<br>
<br>
% Demo to prove to Dongsheng that for loops can be faster.<br>
% Initialize an array.<br>
myArray = zeros (512);<br>
<br>
% Generate a list of 1000 random coordinates<br>
% that we will assign a value of 1 to.<br>
co = int32(randi(512, [1000 2]));<br>
rows = co(:,1);<br>
cols = co(:,2);<br>
<br>
% First assign values using for loop.<br>
% Start timer<br>
tStart1 = tic;<br>
for k = 1 : size(co, 1)<br>
% Set each point one at a time.<br>
myArray(rows(k), cols(k))=1;<br>
end<br>
tElapsed1 = toc(tStart1);<br>
fprintf('The time for the "for" loop is %f\n', tElapsed1);<br>
% Note the short time.<br>
<br>
% Now do again, this time timing the assignment where we<br>
% first get linear indexes using sub2ind,<br>
% instead of using a for loop.<br>
tStart2 = tic;<br>
linearIndexes = sub2ind([512 512], rows, cols);<br>
myArray(linearIndexes) = 1;<br>
tElapsed2 = toc(tStart2);<br>
fprintf('The time using sub2ind is %f, ', tElapsed2);<br>
fprintf('a factor of %f longer\n', tElapsed2/tElapsed1);<br>
% Note the time of sub2ind is about 212 times as long<br>
% as it was using the for loop.<br>
<br>
<br>
For your case it doesn't really matter  both are faster than a rocket<br>
powered cheetah on steroids, but if time really matters it could be<br>
worth trying it both ways.<br>
ImageAnalyst