Thread Subject: density matrix conversion - vectorization

Subject: density matrix conversion - vectorization

From: uC

Date: 1 Aug, 2008 15:22:09

Message: 1 of 7

the problem is as follows:
- there is a matrix MxN containing density values of some virtual 'entities'
- the matrix needs to be processed and the result should contain the list of
coordinates of 'entities' coresponding to the location of the given density
point in MxN matrix (i.e. cooresponding to the coordinates of the density
point in MxN matrix).

I did it easily using three for loops (one for M, one for N and one for
density value) however it is quite slow since M and N ~=10k. does anyone has
any idea how to VECTORIZE it? I intentionally don't publish my code since I
am asking about the idea (if it is really needed for the discussion I can
post it).

best wishes,
uC

Subject: density matrix conversion - vectorization

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 1 Aug, 2008 15:54:28

Message: 2 of 7

In article <g6v9mp$g0r$1@news.dialog.net.pl>, uC <bla.bla@uc.uc> wrote:
>the problem is as follows:
>- there is a matrix MxN containing density values of some virtual 'entities'
>- the matrix needs to be processed and the result should contain the list of
>coordinates of 'entities' coresponding to the location of the given density
>point in MxN matrix (i.e. cooresponding to the coordinates of the density
>point in MxN matrix).

Your talk of "entities" confuses me.

>I did it easily using three for loops (one for M, one for N and one for
>density value) however it is quite slow since M and N ~=10k. does anyone has
>any idea how to VECTORIZE it? I intentionally don't publish my code since I
>am asking about the idea (if it is really needed for the discussion I can
>post it).

Do you have the image processing toolbox? Have you tried

InEntity = TheMatrix > DensityCutoff;
B = bwboundaries(InEntity); %or some other bwboundaries call

--
This is a Usenet signature block. Please do not quote it when replying
to one of my postings.
http://en.wikipedia.org/wiki/Signature_block

Subject: density matrix conversion - vectorization

From: D Stobbe

Date: 1 Aug, 2008 15:59:03

Message: 3 of 7

UC-

So what do you actually want to do? It sounds like you
want:

Given M:

M = [10, 20; 30, 40]

V = [1,1,10;
     1,2,20;
     2,1,30;
     2,2,40]

????????????

Subject: density matrix conversion - vectorization

From: uC

Date: 1 Aug, 2008 16:12:26

Message: 4 of 7

"Walter Roberson" <roberson@ibd.nrc-cnrc.gc.ca> wrote in message
news:g6vbjk$rbn$1@canopus.cc.umanitoba.ca...
> In article <g6v9mp$g0r$1@news.dialog.net.pl>, uC <bla.bla@uc.uc> wrote:
>>the problem is as follows:
>>- there is a matrix MxN containing density values of some virtual
>>'entities'
>>- the matrix needs to be processed and the result should contain the list
>>of
>>coordinates of 'entities' coresponding to the location of the given
>>density
>>point in MxN matrix (i.e. cooresponding to the coordinates of the density
>>point in MxN matrix).
>
> Your talk of "entities" confuses me.

sorry for that. ok, the entity is a human, ane the MxN matrix is population
density (one matrix element represents 25x25 meters pixel). so, form MxN
matrix I would like to generate "real" people with their locations being
randomly distributed within each 25x25 pixel

>>I did it easily using three for loops (one for M, one for N and one for
>>density value) however it is quite slow since M and N ~=10k. does anyone
>>has
>>any idea how to VECTORIZE it? I intentionally don't publish my code since
>>I
>>am asking about the idea (if it is really needed for the discussion I can
>>post it).
>
> Do you have the image processing toolbox? Have you tried
>
> InEntity = TheMatrix > DensityCutoff;
> B = bwboundaries(InEntity); %or some other bwboundaries call

thanks for this example, however I don't think that's what I am looking for

best wishes,
uC

Subject: density matrix conversion - vectorization

From: uC

Date: 1 Aug, 2008 16:25:26

Message: 5 of 7

"D Stobbe" <dstobbe101@yahoo.com> wrote in message
news:g6vbs7$b4g$1@fred.mathworks.com...
> UC-
>
> So what do you actually want to do? It sounds like you
> want:
>
> Given M:
>
> M = [10, 20; 30, 40]
>
> V = [1,1,10;
> 1,2,20;
> 2,1,30;
> 2,2,40]

thanks for your reply
no, MxN is a population density matrix and based on that I would like to
generate people locations

so for your M matrix V should contain (x,y) coordinates of:
10 different people in (1,1) pixel
20 different people in (2,1) pixel
30 different people in (1,2) pixel
40 different people in (2,2) pixel

so it will be 100x2 matrix as for size

(x,y) coordinates will be randomly generated within pixel size (in my case
25x25m) but it is a different story

best wishes,
uC

Subject: density matrix conversion - vectorization

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 1 Aug, 2008 17:36:52

Message: 6 of 7

In article <g6vcmb$hqn$1@news.dialog.net.pl>, uC <bla.bla@uc.uc> wrote:

>sorry for that. ok, the entity is a human, ane the MxN matrix is population
>density (one matrix element represents 25x25 meters pixel). so, form MxN
>matrix I would like to generate "real" people with their locations being
>randomly distributed within each 25x25 pixel

Presuming that the density matrix (call it D) contains counts
rather than probabilities:

P = sum(D(:)); %total population
randpos = rand(P,2);
rawx = randpos(:,1);
rawy = randpos(:,2);
groupedx = reshape(mat2cell(rawx,D(:)), size(D));
groupedy = reshape(mat2cell(rawy,D(:)), size(D));

Then for each entry D(Q,R), groupedx{Q,R} are the x coordinates
and groupedy{Q,R} are the y coordinates, each cell relative.

If you need to convert to absolute coordinates:

[coloff,rowoff] = meshgrid(0:size(D,2)-1,0:size(D,1)-1);
absx = cellfun(@(c,d) c+d, groupedx, num2cell(rowoff), 'Uniform', 0);
absy = cellfun(@(c,d) c+d, groupedy, num2cell(coloff), 'Uniform', 0);

And if you need to,

xypairs = cellfun(@(c,d) [c,d], absx, absy, 'Uniform',0);

For example, with D = [3 0 5;9 1 2] this produced
xypairs =

    [3x2 double] [0x2 double] [5x2 double]
    [9x2 double] [1x2 double] [2x2 double]

and for this run,

>> xypairs{2,3}

          1.26297128454014 2.08443584551091
          1.65407909847678 2.3997826490989


Cross check:

allxy = cat(1,xypairs{:});
scatter(allxy(:,1),allxy(:,2));


Note: it might turn out to be faster to use a loop for the
conversion to absolute coordinates and forming the xy pairs.
You could do it as a single loop over 1:numels(D)
--
  "The human mind is so strangely capricious, that, when freed from
  the pressure of real misery, it becomes open and sensitive to the
  ideal apprehension of ideal calamities." -- Sir Walter Scott

Subject: density matrix conversion - vectorization

From: uC

Date: 1 Aug, 2008 19:21:49

Message: 7 of 7

"Walter Roberson" <roberson@ibd.nrc-cnrc.gc.ca> wrote in message
news:g6vhjk$6m4$1@canopus.cc.umanitoba.ca...
> In article <g6vcmb$hqn$1@news.dialog.net.pl>, uC <bla.bla@uc.uc> wrote:
>
>>sorry for that. ok, the entity is a human, ane the MxN matrix is
>>population
>>density (one matrix element represents 25x25 meters pixel). so, form MxN
>>matrix I would like to generate "real" people with their locations being
>>randomly distributed within each 25x25 pixel
>
> Presuming that the density matrix (call it D) contains counts
> rather than probabilities:
>
> P = sum(D(:)); %total population
> randpos = rand(P,2);
> rawx = randpos(:,1);
> rawy = randpos(:,2);
> groupedx = reshape(mat2cell(rawx,D(:)), size(D));
> groupedy = reshape(mat2cell(rawy,D(:)), size(D));
>
> Then for each entry D(Q,R), groupedx{Q,R} are the x coordinates
> and groupedy{Q,R} are the y coordinates, each cell relative.
>
> If you need to convert to absolute coordinates:
>
> [coloff,rowoff] = meshgrid(0:size(D,2)-1,0:size(D,1)-1);
> absx = cellfun(@(c,d) c+d, groupedx, num2cell(rowoff), 'Uniform', 0);
> absy = cellfun(@(c,d) c+d, groupedy, num2cell(coloff), 'Uniform', 0);
>
> And if you need to,
>
> xypairs = cellfun(@(c,d) [c,d], absx, absy, 'Uniform',0);
>
> For example, with D = [3 0 5;9 1 2] this produced
> xypairs =
>
> [3x2 double] [0x2 double] [5x2 double]
> [9x2 double] [1x2 double] [2x2 double]
>
> and for this run,
>
>>> xypairs{2,3}
>
> 1.26297128454014 2.08443584551091
> 1.65407909847678 2.3997826490989
>
>
> Cross check:
>
> allxy = cat(1,xypairs{:});
> scatter(allxy(:,1),allxy(:,2));
>
>
> Note: it might turn out to be faster to use a loop for the
> conversion to absolute coordinates and forming the xy pairs.
> You could do it as a single loop over 1:numels(D)

thanks a lot, I will analyze your example tomorrow
density matrix does'not contain probabilities directly, but because the
pixels are relatively small most of them contain non integer number of
people, mostly <1 thus some probability needs to be involved - but it can be
easily converted to your "count matrix" case
anyway, thanks again
BW
uC

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
conversion table vick 18 Feb, 2009 17:49:27
rssFeed for this Thread

Contact us at files@mathworks.com