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:
Vectorised approach to fill the array

Subject: Vectorised approach to fill the array

From: Liana

Date: 19 Apr, 2011 08:21:04

Message: 1 of 7

Hello,

i'd like to fill the array in the vectorised way.

let's say there are indices of 3 points, and each point has 3 neighbor points.
points = [1; 2; 4];
neighbors = [1 4 5;...
                   1 2 3;...
                   3 4 5];

the array must be filled as follows:
m(1,1) = 1; m(1,4) = 1; m(1,5) = 1;
m(2,1) = 1; m(2,2) = 1; m(2,3) = 1;
m(4,3) = 1; m(4,4) = 1; m(4,5) = 1;

i was thinking about 'reshape' function in order to transform the array 'neighbors' into the vector. however, it's not enough to solve the problem, because each cell in 'neighbors' corresponds to a certain row in 'points'.

i hope my question is clear. thank you very much.

Subject: Vectorised approach to fill the array

From: Matt J

Date: 19 Apr, 2011 11:08:04

Message: 2 of 7

"Liana" wrote in message <iojglg$p5f$1@fred.mathworks.com>...
>
>
> the array must be filled as follows:
> m(1,1) = 1; m(1,4) = 1; m(1,5) = 1;
> m(2,1) = 1; m(2,2) = 1; m(2,3) = 1;
> m(4,3) = 1; m(4,4) = 1; m(4,5) = 1;
>
> i was thinking about 'reshape' function in order to transform the array 'neighbors' into the vector. however, it's not enough to solve the problem, because each cell in 'neighbors' corresponds to a certain row in 'points'.
=======================================


Some potentially useful functions:

repmat
sub2ind
accumarray

Subject: Vectorised approach to fill the array

From: Liana

Date: 20 Apr, 2011 06:47:04

Message: 3 of 7

Thank you. I've almost solved the problem using repmat and reshape. However, I've got another problem, for which I opened a new thread, 'cause its quite specific.

"Matt J" wrote in message <iojqek$s7k$1@fred.mathworks.com>...
> "Liana" wrote in message <iojglg$p5f$1@fred.mathworks.com>...
> >
> >
> > the array must be filled as follows:
> > m(1,1) = 1; m(1,4) = 1; m(1,5) = 1;
> > m(2,1) = 1; m(2,2) = 1; m(2,3) = 1;
> > m(4,3) = 1; m(4,4) = 1; m(4,5) = 1;
> >
> > i was thinking about 'reshape' function in order to transform the array 'neighbors' into the vector. however, it's not enough to solve the problem, because each cell in 'neighbors' corresponds to a certain row in 'points'.
> =======================================
>
>
> Some potentially useful functions:
>
> repmat
> sub2ind
> accumarray

Subject: Vectorised approach to fill the array

From: Abhinav Gaur

Date: 20 Apr, 2011 13:30:20

Message: 4 of 7

"Liana" wrote in message <iolvh8$411$1@fred.mathworks.com>...
> Thank you. I've almost solved the problem using repmat and reshape. However, I've got another problem, for which I opened a new thread, 'cause its quite specific.
>
> "Matt J" wrote in message <iojqek$s7k$1@fred.mathworks.com>...
> > "Liana" wrote in message <iojglg$p5f$1@fred.mathworks.com>...
> > >
> > >
> > > the array must be filled as follows:
> > > m(1,1) = 1; m(1,4) = 1; m(1,5) = 1;
> > > m(2,1) = 1; m(2,2) = 1; m(2,3) = 1;
> > > m(4,3) = 1; m(4,4) = 1; m(4,5) = 1;

Given below is the solution.

%%%%%%%%%%%%%% SOLUTION %%%%%%%%%%%%%%%%%%%%%
% As given
points = [1;2;4];
neighbours = [1 4 5;1 2 3;3 4 5];

% Assuming parameters of problem (can be generalised later)

tot_nof_p = 4; % Total number of points in domain,
           % of which only three are selected for neighbour tracking,
           % e.g
           % The row vector, 'points' can have any of the following as its
           % entries, : 1, 2, 3, 4 of which only 1,2,4 form the vector
           % 'poitns' at this instant
           
tot_nof_n = 5; % Total number of possible neighbours in domain for a point, at
           % any instant, of which only three finally qualify as neighbours
           % e.g.
           % At chosen instant, any of the numbers 1,2,3,4,5 can be a
           % neighbour of any point, but we select any three of them at
           % an instant for a single point
           
% Initialise the matrix 'ANS' with zeros, I am assuming that you
% require ones only at positions you have specified in problem
% (i.e. <1,1>, <1,4>, <1,5>, <2,1>, <1,2>, .... etc) and rest of
% them will remain zeros
ANS = zeros(tot_nof_p, tot_nof_n);

size_p = size(points);% will use later
size_n = size(neighbours);% will use later

% Vectorisation Part
idx = points(:,ones(1,size_n(2))) + tot_nof_p * ((neighbours) - (ones(size_n)));
% idx carries the indices of ANS where I need to replace zeros with ones

ANS(idx) = 1;
% DONE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Abhinav

Subject: Vectorised approach to fill the array

From: Abhinav Gaur

Date: 20 Apr, 2011 13:40:05

Message: 5 of 7

"Liana" wrote in message <iolvh8$411$1@fred.mathworks.com>...
> Thank you. I've almost solved the problem using repmat and reshape. However, I've got another problem, for which I opened a new thread, 'cause its quite specific.
>
> "Matt J" wrote in message <iojqek$s7k$1@fred.mathworks.com>...
> > "Liana" wrote in message <iojglg$p5f$1@fred.mathworks.com>...
> > >
> > >
> > > the array must be filled as follows:
> > > m(1,1) = 1; m(1,4) = 1; m(1,5) = 1;
> > > m(2,1) = 1; m(2,2) = 1; m(2,3) = 1;
> > > m(4,3) = 1; m(4,4) = 1; m(4,5) = 1;

Given below is the solution.

%%%%%%%%%%%%%% SOLUTION %%%%%%%%%%%%%%%%%%%%%
% As given
points = [1;2;4];
neighbours = [1 4 5;1 2 3;3 4 5];

% Assuming parameters of problem (can be generalised later)

tot_nof_p = 4; % Total number of points in domain,
           % of which only three are selected for neighbour tracking,
           % e.g
           % The row vector, 'points' can have any of the following as its
           % entries, : 1, 2, 3, 4 of which only 1,2,4 form the vector
           % 'poitns' at this instant
           
tot_nof_n = 5; % Total number of possible neighbours in domain for a point, at
           % any instant, of which only three finally qualify as neighbours
           % e.g.
           % At chosen instant, any of the numbers 1,2,3,4,5 can be a
           % neighbour of any point, but we select any three of them at
           % an instant for a single point
           
% Initialise the matrix 'ANS' with zeros, I am assuming that you
% require ones only at positions you have specified in problem
% (i.e. <1,1>, <1,4>, <1,5>, <2,1>, <1,2>, .... etc) and rest of
% them will remain zeros
ANS = zeros(tot_nof_p, tot_nof_n);

size_p = size(points);% will use later
size_n = size(neighbours);% will use later

% Vectorisation Part
idx = points(:,ones(1,size_n(2))) + tot_nof_p * ((neighbours) - (ones(size_n)));
% idx carries the indices of ANS where I need to replace zeros with ones

ANS(idx) = 1;
% DONE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


The reshape and repmat commands are slower .. especially for huge data sets .. vectorised code for data, (anything more than 10000 entries ).... is bound to give u a time improvement by a factor of at least two orders ... let me know how it works ... or if ive understood the problem correctly ... for this small example, u can check the times take by the code using reshape/repmat and this vectorised code with help of tic:toc functions.

Abhinav

Subject: Vectorised approach to fill the array

From: Liana

Date: 20 Apr, 2011 19:28:05

Message: 6 of 7

Thanks, Abhinav! It works perfectly. However, I also want to finish my variant to compare the execution time. In my variant, I transformed all the data into two vectors of indices, e.g. points = [1; 1; 3; 5; 5]; and neighbours = [2; 4; 5; 8; 9]. And each pair, e.g. points(1) x neighbours(1), corresponds to the cell in matrix ANS, which must be equal to 1. As I noticed, I cannot run ANS(points,neighbours)=1. Do I need to linearize these indices? Thanks.

"Abhinav Gaur" wrote in message <iomnnl$bvk$1@fred.mathworks.com>...
> "Liana" wrote in message <iolvh8$411$1@fred.mathworks.com>...
> > Thank you. I've almost solved the problem using repmat and reshape. However, I've got another problem, for which I opened a new thread, 'cause its quite specific.
> >
> > "Matt J" wrote in message <iojqek$s7k$1@fred.mathworks.com>...
> > > "Liana" wrote in message <iojglg$p5f$1@fred.mathworks.com>...
> > > >
> > > >
> > > > the array must be filled as follows:
> > > > m(1,1) = 1; m(1,4) = 1; m(1,5) = 1;
> > > > m(2,1) = 1; m(2,2) = 1; m(2,3) = 1;
> > > > m(4,3) = 1; m(4,4) = 1; m(4,5) = 1;
>
> Given below is the solution.
>
> %%%%%%%%%%%%%% SOLUTION %%%%%%%%%%%%%%%%%%%%%
> % As given
> points = [1;2;4];
> neighbours = [1 4 5;1 2 3;3 4 5];
>
> % Assuming parameters of problem (can be generalised later)
>
> tot_nof_p = 4; % Total number of points in domain,
> % of which only three are selected for neighbour tracking,
> % e.g
> % The row vector, 'points' can have any of the following as its
> % entries, : 1, 2, 3, 4 of which only 1,2,4 form the vector
> % 'poitns' at this instant
>
> tot_nof_n = 5; % Total number of possible neighbours in domain for a point, at
> % any instant, of which only three finally qualify as neighbours
> % e.g.
> % At chosen instant, any of the numbers 1,2,3,4,5 can be a
> % neighbour of any point, but we select any three of them at
> % an instant for a single point
>
> % Initialise the matrix 'ANS' with zeros, I am assuming that you
> % require ones only at positions you have specified in problem
> % (i.e. <1,1>, <1,4>, <1,5>, <2,1>, <1,2>, .... etc) and rest of
> % them will remain zeros
> ANS = zeros(tot_nof_p, tot_nof_n);
>
> size_p = size(points);% will use later
> size_n = size(neighbours);% will use later
>
> % Vectorisation Part
> idx = points(:,ones(1,size_n(2))) + tot_nof_p * ((neighbours) - (ones(size_n)));
> % idx carries the indices of ANS where I need to replace zeros with ones
>
> ANS(idx) = 1;
> % DONE
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
>
> The reshape and repmat commands are slower .. especially for huge data sets .. vectorised code for data, (anything more than 10000 entries ).... is bound to give u a time improvement by a factor of at least two orders ... let me know how it works ... or if ive understood the problem correctly ... for this small example, u can check the times take by the code using reshape/repmat and this vectorised code with help of tic:toc functions.
>
> Abhinav

Subject: Vectorised approach to fill the array

From: Liana

Date: 20 Apr, 2011 19:37:04

Message: 7 of 7

Ok, I solved it:
idx = sub2ind(size(ANS), points', neighbors');
ANS(idx) = 1;

"Liana" wrote in message <iojglg$p5f$1@fred.mathworks.com>...
> Hello,
>
> i'd like to fill the array in the vectorised way.
>
> let's say there are indices of 3 points, and each point has 3 neighbor points.
> points = [1; 2; 4];
> neighbors = [1 4 5;...
> 1 2 3;...
> 3 4 5];
>
> the array must be filled as follows:
> m(1,1) = 1; m(1,4) = 1; m(1,5) = 1;
> m(2,1) = 1; m(2,2) = 1; m(2,3) = 1;
> m(4,3) = 1; m(4,4) = 1; m(4,5) = 1;
>
> i was thinking about 'reshape' function in order to transform the array 'neighbors' into the vector. however, it's not enough to solve the problem, because each cell in 'neighbors' corresponds to a certain row in 'points'.
>
> i hope my question is clear. thank you very much.

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