<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173629</link>
    <title>MATLAB Central Newsreader - density matrix conversion - vectorization</title>
    <description>Feed for thread: density matrix conversion - vectorization</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 01 Aug 2008 15:22:09 -0400</pubDate>
      <title>density matrix conversion - vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173629#446765</link>
      <author>uC</author>
      <description>the problem is as follows:&lt;br&gt;
- there is a matrix MxN containing density values of some virtual 'entities'&lt;br&gt;
- the matrix needs to be processed and the result should contain the list of &lt;br&gt;
coordinates of 'entities' coresponding to the location of the given density &lt;br&gt;
point in MxN matrix (i.e. cooresponding to the coordinates of the density &lt;br&gt;
point in MxN matrix).&lt;br&gt;
&lt;br&gt;
I did it easily using three for loops (one for M, one for N and one for &lt;br&gt;
density value) however it is quite slow since M and N ~=10k. does anyone has &lt;br&gt;
any idea how to VECTORIZE it? I intentionally don't publish my code since I &lt;br&gt;
am asking about the idea (if it is really needed for the discussion I can &lt;br&gt;
post it).&lt;br&gt;
&lt;br&gt;
best wishes,&lt;br&gt;
uC </description>
    </item>
    <item>
      <pubDate>Fri, 01 Aug 2008 15:54:28 -0400</pubDate>
      <title>Re: density matrix conversion - vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173629#446781</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;g6v9mp$g0r$1@news.dialog.net.pl&amp;gt;, uC &amp;lt;bla.bla@uc.uc&amp;gt; wrote:&lt;br&gt;
&amp;gt;the problem is as follows:&lt;br&gt;
&amp;gt;- there is a matrix MxN containing density values of some virtual 'entities'&lt;br&gt;
&amp;gt;- the matrix needs to be processed and the result should contain the list of &lt;br&gt;
&amp;gt;coordinates of 'entities' coresponding to the location of the given density &lt;br&gt;
&amp;gt;point in MxN matrix (i.e. cooresponding to the coordinates of the density &lt;br&gt;
&amp;gt;point in MxN matrix).&lt;br&gt;
&lt;br&gt;
Your talk of &quot;entities&quot; confuses me.&lt;br&gt;
&lt;br&gt;
&amp;gt;I did it easily using three for loops (one for M, one for N and one for &lt;br&gt;
&amp;gt;density value) however it is quite slow since M and N ~=10k. does anyone has &lt;br&gt;
&amp;gt;any idea how to VECTORIZE it? I intentionally don't publish my code since I &lt;br&gt;
&amp;gt;am asking about the idea (if it is really needed for the discussion I can &lt;br&gt;
&amp;gt;post it).&lt;br&gt;
&lt;br&gt;
Do you have the image processing toolbox? Have you tried&lt;br&gt;
&lt;br&gt;
InEntity = TheMatrix &amp;gt; DensityCutoff;&lt;br&gt;
B = bwboundaries(InEntity);     %or some other bwboundaries call&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
This is a Usenet signature block. Please do not quote it when replying&lt;br&gt;
to one of my postings.&lt;br&gt;
&lt;a href=&quot;http://en.wikipedia.org/wiki/Signature_block&quot;&gt;http://en.wikipedia.org/wiki/Signature_block&lt;/a&gt;</description>
    </item>
    <item>
      <pubDate>Fri, 01 Aug 2008 15:59:03 -0400</pubDate>
      <title>Re: density matrix conversion - vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173629#446783</link>
      <author>D Stobbe</author>
      <description>UC-&lt;br&gt;
&lt;br&gt;
So what do you actually want to do?  It sounds like you &lt;br&gt;
want:&lt;br&gt;
&lt;br&gt;
Given M:&lt;br&gt;
&lt;br&gt;
M = [10, 20; 30, 40]&lt;br&gt;
&lt;br&gt;
V = [1,1,10;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1,2,20;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2,1,30;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2,2,40]&lt;br&gt;
&lt;br&gt;
????????????</description>
    </item>
    <item>
      <pubDate>Fri, 01 Aug 2008 16:12:26 -0400</pubDate>
      <title>Re: density matrix conversion - vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173629#446793</link>
      <author>uC</author>
      <description>&quot;Walter Roberson&quot; &amp;lt;roberson@ibd.nrc-cnrc.gc.ca&amp;gt; wrote in message &lt;br&gt;
news:g6vbjk$rbn$1@canopus.cc.umanitoba.ca...&lt;br&gt;
&amp;gt; In article &amp;lt;g6v9mp$g0r$1@news.dialog.net.pl&amp;gt;, uC &amp;lt;bla.bla@uc.uc&amp;gt; wrote:&lt;br&gt;
&amp;gt;&amp;gt;the problem is as follows:&lt;br&gt;
&amp;gt;&amp;gt;- there is a matrix MxN containing density values of some virtual &lt;br&gt;
&amp;gt;&amp;gt;'entities'&lt;br&gt;
&amp;gt;&amp;gt;- the matrix needs to be processed and the result should contain the list &lt;br&gt;
&amp;gt;&amp;gt;of&lt;br&gt;
&amp;gt;&amp;gt;coordinates of 'entities' coresponding to the location of the given &lt;br&gt;
&amp;gt;&amp;gt;density&lt;br&gt;
&amp;gt;&amp;gt;point in MxN matrix (i.e. cooresponding to the coordinates of the density&lt;br&gt;
&amp;gt;&amp;gt;point in MxN matrix).&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Your talk of &quot;entities&quot; confuses me.&lt;br&gt;
&lt;br&gt;
sorry for that. ok, the entity is a human, ane the MxN matrix is population &lt;br&gt;
density (one matrix element represents 25x25 meters pixel). so, form MxN &lt;br&gt;
matrix I would like to generate &quot;real&quot; people with their locations being &lt;br&gt;
randomly distributed within each 25x25 pixel&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt;I did it easily using three for loops (one for M, one for N and one for&lt;br&gt;
&amp;gt;&amp;gt;density value) however it is quite slow since M and N ~=10k. does anyone &lt;br&gt;
&amp;gt;&amp;gt;has&lt;br&gt;
&amp;gt;&amp;gt;any idea how to VECTORIZE it? I intentionally don't publish my code since &lt;br&gt;
&amp;gt;&amp;gt;I&lt;br&gt;
&amp;gt;&amp;gt;am asking about the idea (if it is really needed for the discussion I can&lt;br&gt;
&amp;gt;&amp;gt;post it).&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Do you have the image processing toolbox? Have you tried&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; InEntity = TheMatrix &amp;gt; DensityCutoff;&lt;br&gt;
&amp;gt; B = bwboundaries(InEntity);     %or some other bwboundaries call&lt;br&gt;
&lt;br&gt;
thanks for this example, however I don't think that's what I am looking for&lt;br&gt;
&lt;br&gt;
best wishes,&lt;br&gt;
uC </description>
    </item>
    <item>
      <pubDate>Fri, 01 Aug 2008 16:25:26 -0400</pubDate>
      <title>Re: density matrix conversion - vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173629#446796</link>
      <author>uC</author>
      <description>&quot;D Stobbe&quot; &amp;lt;dstobbe101@yahoo.com&amp;gt; wrote in message &lt;br&gt;
news:g6vbs7$b4g$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; UC-&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; So what do you actually want to do?  It sounds like you&lt;br&gt;
&amp;gt; want:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Given M:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; M = [10, 20; 30, 40]&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; V = [1,1,10;&lt;br&gt;
&amp;gt;     1,2,20;&lt;br&gt;
&amp;gt;     2,1,30;&lt;br&gt;
&amp;gt;     2,2,40]&lt;br&gt;
&lt;br&gt;
thanks for your reply&lt;br&gt;
no, MxN is a population density matrix and based on that I would like to &lt;br&gt;
generate people locations&lt;br&gt;
&lt;br&gt;
so for your M matrix V should contain (x,y) coordinates of:&lt;br&gt;
10 different people in (1,1) pixel&lt;br&gt;
20 different people in (2,1) pixel&lt;br&gt;
30 different people in (1,2) pixel&lt;br&gt;
40 different people in (2,2) pixel&lt;br&gt;
&lt;br&gt;
so it will be 100x2 matrix as for size&lt;br&gt;
&lt;br&gt;
(x,y) coordinates will be randomly generated within pixel size (in my case &lt;br&gt;
25x25m) but it is a different story&lt;br&gt;
&lt;br&gt;
best wishes,&lt;br&gt;
uC </description>
    </item>
    <item>
      <pubDate>Fri, 01 Aug 2008 17:36:52 -0400</pubDate>
      <title>Re: density matrix conversion - vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173629#446815</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;g6vcmb$hqn$1@news.dialog.net.pl&amp;gt;, uC &amp;lt;bla.bla@uc.uc&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt;sorry for that. ok, the entity is a human, ane the MxN matrix is population &lt;br&gt;
&amp;gt;density (one matrix element represents 25x25 meters pixel). so, form MxN &lt;br&gt;
&amp;gt;matrix I would like to generate &quot;real&quot; people with their locations being &lt;br&gt;
&amp;gt;randomly distributed within each 25x25 pixel&lt;br&gt;
&lt;br&gt;
Presuming that the density matrix (call it D) contains counts &lt;br&gt;
rather than probabilities:&lt;br&gt;
&lt;br&gt;
P = sum(D(:));          %total population&lt;br&gt;
randpos = rand(P,2);&lt;br&gt;
rawx = randpos(:,1);&lt;br&gt;
rawy = randpos(:,2);&lt;br&gt;
groupedx = reshape(mat2cell(rawx,D(:)), size(D));&lt;br&gt;
groupedy = reshape(mat2cell(rawy,D(:)), size(D));&lt;br&gt;
&lt;br&gt;
Then for each entry D(Q,R), groupedx{Q,R} are the x coordinates&lt;br&gt;
and groupedy{Q,R} are the y coordinates, each cell relative.&lt;br&gt;
&lt;br&gt;
If you need to convert to absolute coordinates:&lt;br&gt;
&lt;br&gt;
[coloff,rowoff] = meshgrid(0:size(D,2)-1,0:size(D,1)-1);&lt;br&gt;
absx = cellfun(@(c,d) c+d, groupedx, num2cell(rowoff), 'Uniform', 0);&lt;br&gt;
absy = cellfun(@(c,d) c+d, groupedy, num2cell(coloff), 'Uniform', 0);&lt;br&gt;
&lt;br&gt;
And if you need to,&lt;br&gt;
&lt;br&gt;
xypairs = cellfun(@(c,d) [c,d], absx, absy, 'Uniform',0);&lt;br&gt;
&lt;br&gt;
For example, with D = [3 0 5;9 1 2] this produced&lt;br&gt;
xypairs = &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[3x2 double]    [0x2  double]    [5x2 double]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[9x2 double]    [1x2 double]    [2x2 double]&lt;br&gt;
&lt;br&gt;
and for this run,&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; xypairs{2,3}&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1.26297128454014          2.08443584551091&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1.65407909847678           2.3997826490989&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Cross check:&lt;br&gt;
&lt;br&gt;
allxy = cat(1,xypairs{:});&lt;br&gt;
scatter(allxy(:,1),allxy(:,2));&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Note: it might turn out to be faster to use a loop for the&lt;br&gt;
conversion to absolute coordinates and forming the xy pairs.&lt;br&gt;
You could do it as a single loop over 1:numels(D)&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&quot;The human mind is so strangely capricious, that, when freed from&lt;br&gt;
&amp;nbsp;&amp;nbsp;the pressure of real misery, it becomes open and sensitive to the&lt;br&gt;
&amp;nbsp;&amp;nbsp;ideal apprehension of ideal calamities.&quot;          -- Sir Walter Scott</description>
    </item>
    <item>
      <pubDate>Fri, 01 Aug 2008 19:21:49 -0400</pubDate>
      <title>Re: density matrix conversion - vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173629#446838</link>
      <author>uC</author>
      <description>&quot;Walter Roberson&quot; &amp;lt;roberson@ibd.nrc-cnrc.gc.ca&amp;gt; wrote in message &lt;br&gt;
news:g6vhjk$6m4$1@canopus.cc.umanitoba.ca...&lt;br&gt;
&amp;gt; In article &amp;lt;g6vcmb$hqn$1@news.dialog.net.pl&amp;gt;, uC &amp;lt;bla.bla@uc.uc&amp;gt; wrote:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt;sorry for that. ok, the entity is a human, ane the MxN matrix is &lt;br&gt;
&amp;gt;&amp;gt;population&lt;br&gt;
&amp;gt;&amp;gt;density (one matrix element represents 25x25 meters pixel). so, form MxN&lt;br&gt;
&amp;gt;&amp;gt;matrix I would like to generate &quot;real&quot; people with their locations being&lt;br&gt;
&amp;gt;&amp;gt;randomly distributed within each 25x25 pixel&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Presuming that the density matrix (call it D) contains counts&lt;br&gt;
&amp;gt; rather than probabilities:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; P = sum(D(:));          %total population&lt;br&gt;
&amp;gt; randpos = rand(P,2);&lt;br&gt;
&amp;gt; rawx = randpos(:,1);&lt;br&gt;
&amp;gt; rawy = randpos(:,2);&lt;br&gt;
&amp;gt; groupedx = reshape(mat2cell(rawx,D(:)), size(D));&lt;br&gt;
&amp;gt; groupedy = reshape(mat2cell(rawy,D(:)), size(D));&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Then for each entry D(Q,R), groupedx{Q,R} are the x coordinates&lt;br&gt;
&amp;gt; and groupedy{Q,R} are the y coordinates, each cell relative.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; If you need to convert to absolute coordinates:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; [coloff,rowoff] = meshgrid(0:size(D,2)-1,0:size(D,1)-1);&lt;br&gt;
&amp;gt; absx = cellfun(@(c,d) c+d, groupedx, num2cell(rowoff), 'Uniform', 0);&lt;br&gt;
&amp;gt; absy = cellfun(@(c,d) c+d, groupedy, num2cell(coloff), 'Uniform', 0);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; And if you need to,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; xypairs = cellfun(@(c,d) [c,d], absx, absy, 'Uniform',0);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; For example, with D = [3 0 5;9 1 2] this produced&lt;br&gt;
&amp;gt; xypairs =&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;    [3x2 double]    [0x2  double]    [5x2 double]&lt;br&gt;
&amp;gt;    [9x2 double]    [1x2 double]    [2x2 double]&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; and for this run,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; xypairs{2,3}&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;          1.26297128454014          2.08443584551091&lt;br&gt;
&amp;gt;          1.65407909847678           2.3997826490989&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Cross check:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; allxy = cat(1,xypairs{:});&lt;br&gt;
&amp;gt; scatter(allxy(:,1),allxy(:,2));&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Note: it might turn out to be faster to use a loop for the&lt;br&gt;
&amp;gt; conversion to absolute coordinates and forming the xy pairs.&lt;br&gt;
&amp;gt; You could do it as a single loop over 1:numels(D)&lt;br&gt;
&lt;br&gt;
thanks a lot, I will analyze your example tomorrow&lt;br&gt;
density matrix does'not contain probabilities directly, but because the &lt;br&gt;
pixels are relatively small most of them contain non integer number of &lt;br&gt;
people, mostly &amp;lt;1 thus some probability needs to be involved - but it can be &lt;br&gt;
easily converted to your &quot;count matrix&quot; case&lt;br&gt;
anyway, thanks again&lt;br&gt;
BW&lt;br&gt;
uC </description>
    </item>
  </channel>
</rss>

