Thread Subject: Efficient computation: differences of column vectors of matrix

Subject: Efficient computation: differences of column vectors of matrix

From: arun

Date: 1 Jul, 2009 00:17:35

Message: 1 of 8

Hi and thanks for the help,

I have a matrix say

A = [a1,b1,c1,d1; a2,b2,c2,d2; a3,b3,c3,d3; a4,b4,c4,d4]

now, I want the differences across the columns as,

B = [(a1-a2),(a1-a3),(a1-a4),(a2-a3),(a2-a4),(a3-a4);
       (b1-b2),(b1-b3),(b1-b4),(b2-b3),(b2-b4),(b3-b4);
       (c1-c2),(c1-c3),(c1-c4),(c2-c3),(c2-c4),(c3-c4);
       (d1-d2),(d1-d3),(d1-d4),(d2-d3),(d2-d4),(d3-d4)]

i would like to do this efficiently in Matlab because, my A =
200*250000 and so, after doing for each of the 200 columns... B matrix
would be 200*199/2= 19,900 * 250000. I would like to know if it could
be done efficiently (since its across columns). if space is a problem,
i can subdivide the matrix, it shouldn't be an issue.


thanks again,
regards,
arun.

Subject: Efficient computation: differences of column vectors of matrix

From: Bruno Luong

Date: 1 Jul, 2009 06:31:01

Message: 2 of 8

arun <aragorn168b@gm%ail.com> wrote in message <cd6f7a80-0651-417d-ac72-88d6b326e1fe@b14g2000yqd.googlegroups.com>...
> Hi and thanks for the help,
>
> I have a matrix say
>
> A = [a1,b1,c1,d1; a2,b2,c2,d2; a3,b3,c3,d3; a4,b4,c4,d4]
>
> now, I want the differences across the columns as,
>
> B = [(a1-a2),(a1-a3),(a1-a4),(a2-a3),(a2-a4),(a3-a4);
> (b1-b2),(b1-b3),(b1-b4),(b2-b3),(b2-b4),(b3-b4);
> (c1-c2),(c1-c3),(c1-c4),(c2-c3),(c2-c4),(c3-c4);
> (d1-d2),(d1-d3),(d1-d4),(d2-d3),(d2-d4),(d3-d4)]
>
> i would like to do this efficiently in Matlab because, my A =
> 200*250000 and so, after doing for each of the 200 columns... B matrix
> would be 200*199/2= 19,900 * 250000. I would like to know if it could
> be done efficiently (since its across columns). if space is a problem,
> i can subdivide the matrix, it shouldn't be an issue.
>

A=ceil(10*rand(10,4));

J2=nchoosek(1:size(A,2),2);
A(:,J2(:,1))-A(:,J2(:,2))

% Bruno

Subject: Efficient computation: differences of column vectors of matrix

From: arun

Date: 1 Jul, 2009 10:07:08

Message: 3 of 8

On Jul 1, 8:31 am, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> arun <aragorn168b@gm%ail.com> wrote in message <cd6f7a80-0651-417d-ac72-88d6b326e...@b14g2000yqd.googlegroups.com>...
> > Hi and thanks for the help,
>
> > I have a matrix say
>
> > A = [a1,b1,c1,d1; a2,b2,c2,d2; a3,b3,c3,d3; a4,b4,c4,d4]
>
> > now, I want the differences across the columns as,
>
> > B = [(a1-a2),(a1-a3),(a1-a4),(a2-a3),(a2-a4),(a3-a4);
> >        (b1-b2),(b1-b3),(b1-b4),(b2-b3),(b2-b4),(b3-b4);
> >        (c1-c2),(c1-c3),(c1-c4),(c2-c3),(c2-c4),(c3-c4);
> >        (d1-d2),(d1-d3),(d1-d4),(d2-d3),(d2-d4),(d3-d4)]
>
> > i would like to do this efficiently in Matlab because, my A =
> > 200*250000 and so, after doing for each of the 200 columns... B matrix
> > would be 200*199/2= 19,900 * 250000. I would like to know if it could
> > be done efficiently (since its across columns). if space is a problem,
> > i can subdivide the matrix, it shouldn't be an issue.
>
> A=ceil(10*rand(10,4));
>
> J2=nchoosek(1:size(A,2),2);
> A(:,J2(:,1))-A(:,J2(:,2))
>
> % Bruno

Hi Bruno,
thank you for your help. But there seems to be a problem.

The J2 matrix seems to generate the indices properly. But, suppose J2
= [1,1,1,2,2,3; 2,3,4,3,4,4] then, A(:,J2(:,1)) will simply choose a
matrix which has the first column repeated three times and 2nd column
repeated 2 times and third column repeated 1 time... I hope I
explained clear enough to understand what hte problem is...
However, thank you. I will also try to work on it.

regards,
arun.

Subject: Efficient computation: differences of column vectors of matrix

From: arun

Date: 1 Jul, 2009 10:15:06

Message: 4 of 8

On Jul 1, 8:31 am, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> arun <aragorn168b@gm%ail.com> wrote in message <cd6f7a80-0651-417d-ac72-88d6b326e...@b14g2000yqd.googlegroups.com>...
> > Hi and thanks for the help,
>
> > I have a matrix say
>
> > A = [a1,b1,c1,d1; a2,b2,c2,d2; a3,b3,c3,d3; a4,b4,c4,d4]
>
> > now, I want the differences across the columns as,
>
> > B = [(a1-a2),(a1-a3),(a1-a4),(a2-a3),(a2-a4),(a3-a4);
> >        (b1-b2),(b1-b3),(b1-b4),(b2-b3),(b2-b4),(b3-b4);
> >        (c1-c2),(c1-c3),(c1-c4),(c2-c3),(c2-c4),(c3-c4);
> >        (d1-d2),(d1-d3),(d1-d4),(d2-d3),(d2-d4),(d3-d4)]
>
> > i would like to do this efficiently in Matlab because, my A =
> > 200*250000 and so, after doing for each of the 200 columns... B matrix
> > would be 200*199/2= 19,900 * 250000. I would like to know if it could
> > be done efficiently (since its across columns). if space is a problem,
> > i can subdivide the matrix, it shouldn't be an issue.
>
> A=ceil(10*rand(10,4));
>
> J2=nchoosek(1:size(A,2),2);
> A(:,J2(:,1))-A(:,J2(:,2))
>
> % Bruno

Hi I just found the modification which works. I just have to check if
it works faster for bigger matrices.
thank you.

its just,
A(J2(:,1),:) - A(J2(:,2),:)

best,
arun.

Subject: Efficient computation: differences of column vectors of matrix

From: Bruno Luong

Date: 1 Jul, 2009 11:48:01

Message: 5 of 8

arun <aragorn168b@gmail.com> wrote in message <63985d2a-adf8-47c9-aaa3-52e0a1cb9ede@37g2000yqp.googlegroups.com>...

>
> its just,
> A(J2(:,1),:) - A(J2(:,2),:)


Unless I'm mistaking, this is NOT what you have described in the example in the post #1. But anyway you seem to know what you want.

Bruno

Subject: Efficient computation: differences of column vectors of matrix

From: oruganti murthy

Date: 1 Jul, 2009 12:10:03

Message: 6 of 8

Hi
I'm also having a similar problem.
I am having two matrices containing 150 size vectors. say A is 5000x150 aand B is 1500x150. I want to find pairwise euclidean distance between vectors of A and B. i.e. I want 5000x1500 matrix.
Is there any efficient way to do it.
If not distance, even absolute difference is fine.
Because at next level these numbers (5000 changes to 25000)also increase and I am encountering OUT of MEMORY or very time consuming problems.
Thanking you very much,


regards,
ramana

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h2fidh$mpl$1@fred.mathworks.com>...
> arun <aragorn168b@gmail.com> wrote in message <63985d2a-adf8-47c9-aaa3-52e0a1cb9ede@37g2000yqp.googlegroups.com>...
>
> >
> > its just,
> > A(J2(:,1),:) - A(J2(:,2),:)
>
>
> Unless I'm mistaking, this is NOT what you have described in the example in the post #1. But anyway you seem to know what you want.
>
> Bruno

Subject: Efficient computation: differences of column vectors of matrix

From: Bruno Luong

Date: 1 Jul, 2009 12:36:01

Message: 7 of 8

"oruganti murthy" <omurthy@yahoo.com> wrote in message <h2fjmr$gdn$1@fred.mathworks.com>...
> Hi
> I'm also having a similar problem.
> I am having two matrices containing 150 size vectors. say A is 5000x150 aand B is 1500x150. I want to find pairwise euclidean distance between vectors of A and B. i.e. I want 5000x1500 matrix.
> Is there any efficient way to do it.
> If not distance, even absolute difference is fine.
> Because at next level these numbers (5000 changes to 25000)also increase and I am encountering OUT of MEMORY or very time consuming problems.
> Thanking you very much,
>

Take a look at function BSXFUN

Bruno

Subject: Efficient computation: differences of column vectors of matrix

From: arun

Date: 2 Jul, 2009 11:33:47

Message: 8 of 8

On Jul 1, 2:36 pm, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> "oruganti murthy" <omur...@yahoo.com> wrote in message <h2fjmr$gd...@fred.mathworks.com>...
> > Hi
> > I'm also having a similar problem.
> > I am having two matrices containing 150 size vectors. say A is 5000x150 aand B is 1500x150. I want to find pairwise euclidean distance between vectors of A and B. i.e. I want 5000x1500 matrix.
> > Is there any efficient way to do it.
> > If not distance, even absolutedifferenceis fine.
> > Because at next level these numbers (5000 changes to 25000)also increase and I am encountering OUT of MEMORY or very time consuming problems.
> > Thanking you very much,
>
> Take a look at function BSXFUN
>
> Bruno

Hi Bruno,
yes, my representation of the final matrix was incorrect. thank you,
arun.

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
pairwise distance Ramana murthy 1 Jul, 2009 08:14:05
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com