Got Questions? Get Answers.
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:
looping issue

Subject: looping issue

From: Kate

Date: 28 Sep, 2010 04:57:04

Message: 1 of 8

hi guys,

would like some assistance please.

I have a matrix array of 4 Lines with their line endpoints in the form [ x1 y1 x2 y2] as follows in the array:

Lines = [ 23 11 45 67;
              99 45 2 32;
              43 71 88 61;
              6 35 56 14]

I want to do the computation : (x2 - x1)*(x4-x3) + (y2-y1)*(y4-y3) for all combinations of lines. Note that [ x1 y1 x2 y2] is for the first line in the computations, whilst [ x3 y3 x4 y4] is for the second line.

The possible combinations are as follows :
L12
L13
L14
L21
L23
L24
L31
L32
L34
L41
L42
L43

The computations I do for L12, L13 and L14 are as follows:

 For_Lines_1_and_2 = (Lines(1,3)-Lines(1,1)*Lines(2,3)-Lines(2,1)) +
                                (Lines(1,4)-Lines(1,2)*Lines(2,4)-Lines(2,2))
  For_Lines_1_and_3 = (Lines(1,3)-Lines(1,1)*Lines(3,3)-Lines(3,1)) +
                                 (Lines(1,4)-Lines(1,2)*Lines(3,4)-Lines(3,2))
  For_Lines_1_and_4 = (Lines(1,3)-Lines(1,1)*Lines(4,3)-Lines(4,1)) +
                                 (Lines(1,4)-Lines(1,2)*Lines(4,4)-Lines(4,2))


How can I do computations an easier way from L12 all the way to L43 using some type of looping?

please help
kate

Subject: looping issue

From: Roger Stafford

Date: 28 Sep, 2010 05:43:23

Message: 2 of 8

"Kate " <chino_tones@hotmail.com> wrote in message <i7rsj0$1ln$1@fred.mathworks.com>...
> hi guys,
>
> would like some assistance please.
>
> I have a matrix array of 4 Lines with their line endpoints in the form [ x1 y1 x2 y2] as follows in the array:
>
> Lines = [ 23 11 45 67;
> 99 45 2 32;
> 43 71 88 61;
> 6 35 56 14]
>
> I want to do the computation : (x2 - x1)*(x4-x3) + (y2-y1)*(y4-y3) for all combinations of lines. Note that [ x1 y1 x2 y2] is for the first line in the computations, whilst [ x3 y3 x4 y4] is for the second line.
>
> The possible combinations are as follows :
> L12
> L13
> L14
> L21
> L23
> L24
> L31
> L32
> L34
> L41
> L42
> L43
>
> The computations I do for L12, L13 and L14 are as follows:
>
> For_Lines_1_and_2 = (Lines(1,3)-Lines(1,1)*Lines(2,3)-Lines(2,1)) +
> (Lines(1,4)-Lines(1,2)*Lines(2,4)-Lines(2,2))
> For_Lines_1_and_3 = (Lines(1,3)-Lines(1,1)*Lines(3,3)-Lines(3,1)) +
> (Lines(1,4)-Lines(1,2)*Lines(3,4)-Lines(3,2))
> For_Lines_1_and_4 = (Lines(1,3)-Lines(1,1)*Lines(4,3)-Lines(4,1)) +
> (Lines(1,4)-Lines(1,2)*Lines(4,4)-Lines(4,2))
>
>
> How can I do computations an easier way from L12 all the way to L43 using some type of looping?
>
> please help
> kate
- - - - - - - - - -
 A = Lines(:,1)-Lines(:,3);
 B = Lines(:,2)-Lines(:,4);
 M = A*A.'+B*B.';
 M(1:n=1:n^2) = [];

Roger Stafford

Subject: looping issue

From: Kate

Date: 28 Sep, 2010 05:53:06

Message: 3 of 8


> A = Lines(:,1)-Lines(:,3);
> B = Lines(:,2)-Lines(:,4);
> M = A*A.'+B*B.';
> M(1:n=1:n^2) = [];


hi roger,

thanks for the help. However, I'm not sure if is the answers i'm looking for since I cant seem to understand what the 4th line means as you have a slight error in there somewhere which i cant seem to fix.

thanks
kate

Subject: looping issue

From: Roger Stafford

Date: 28 Sep, 2010 05:56:09

Message: 4 of 8

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i7rv9r$rvf$1@fred.mathworks.com>...
> A = Lines(:,1)-Lines(:,3);
> B = Lines(:,2)-Lines(:,4);
> M = A*A.'+B*B.';
> M(1:n=1:n^2) = [];
>
> Roger Stafford
- - - - - - - - -
  Oops a typo! I meant

 M(1:n+1:n^2) = [];

in the last equation. {n is equal to size(Lines,1)}

Roger Stafford

Subject: looping issue

From: Kate

Date: 28 Sep, 2010 06:15:36

Message: 5 of 8

hello again roger,

the first 3 values of my answer are supposed to be:
[-430 -2697 -1371...........................].

these are obtained from:

  For_Lines_1_and_2 = (Lines(1,3)-Lines(1,1)*Lines(2,3)-Lines(2,1)) +
                                 (Lines(1,4)-Lines(1,2)*Lines(2,4)-Lines(2,2))

  For_Lines_1_and_3 = (Lines(1,3)-Lines(1,1)*Lines(3,3)-Lines(3,1)) +
                                 (Lines(1,4)-Lines(1,2)*Lines(3,4)-Lines(3,2))

  For_Lines_1_and_4 = (Lines(1,3)-Lines(1,1)*Lines(4,3)-Lines(4,1)) +
                                 (Lines(1,4)-Lines(1,2)*Lines(4,4)-Lines(4,2))

I dont seem to get this with your solution. So I'm asking for some more help.

thanks a lot
kate

Subject: looping issue

From: Roger Stafford

Date: 28 Sep, 2010 07:55:07

Message: 6 of 8

"Kate " <chino_tones@hotmail.com> wrote in message <i7s168$e0$1@fred.mathworks.com>...
> hello again roger,
>
> the first 3 values of my answer are supposed to be:
> [-430 -2697 -1371...........................].
>
> these are obtained from:
>
> For_Lines_1_and_2 = (Lines(1,3)-Lines(1,1)*Lines(2,3)-Lines(2,1)) +
> (Lines(1,4)-Lines(1,2)*Lines(2,4)-Lines(2,2))
>
> For_Lines_1_and_3 = (Lines(1,3)-Lines(1,1)*Lines(3,3)-Lines(3,1)) +
> (Lines(1,4)-Lines(1,2)*Lines(3,4)-Lines(3,2))
>
> For_Lines_1_and_4 = (Lines(1,3)-Lines(1,1)*Lines(4,3)-Lines(4,1)) +
> (Lines(1,4)-Lines(1,2)*Lines(4,4)-Lines(4,2))
>
> I dont seem to get this with your solution. So I'm asking for some more help.
>
> thanks a lot
> kate
- - - - - - - - - -
  You have misplaced the parentheses in your expressions. You stated that you wanted to compute (x2-x1)*(x4-x3) + (y2-y1)*(y4-y3) which is the dot product of the two vectors P2-P1 and P4-P3 where P1 = [x1,y1], P2 = [x2,y2], P3 = [x3,y3], and P4 = [x4,y4]. However that isn't what you get with the following expression where two crucial parentheses are missing:

 For_Lines_1_and_2 = (Lines(1,3)-Lines(1,1)*Lines(2,3)-Lines(2,1)) +
                     (Lines(1,4)-Lines(1,2)*Lines(2,4)-Lines(2,2))

You need this:

 For_Lines_1_and_2 = (Lines(1,3)-Lines(1,1))*(Lines(2,3)-Lines(2,1)) +
                     (Lines(1,4)-Lines(1,2))*(Lines(2,4)-Lines(2,2))

  It's your own calculations that are in error, Kate.

  Out of haste I calculated A and B backwards, though the answer should still be correct. However I should have written

 A = Lines(:,3)-Lines(:,1);
 B = Lines(:,4)-Lines(:,2);

to make it easier to understand.

Roger Stafford

Subject: looping issue

From: Kate

Date: 28 Sep, 2010 09:10:05

Message: 7 of 8

appreciate it roger.

thank you
kate

Subject: looping issue

From: Roger Stafford

Date: 28 Sep, 2010 09:27:02

Message: 8 of 8

"Kate " <chino_tones@hotmail.com> wrote in message <i7sbdd$b0o$1@fred.mathworks.com>...
> appreciate it roger.
>
> thank you
> kate
- - - - - - - -
  Here is a slightly more compact method:

 n = size(Lines,1);
 V = Lines(:,3:4)-Lines(:,1:2); % n rows of 2-element vectors in x-y plane
 M = V*V.'; % Compute all pairs of dot products
 M(1:n+1:n^2) = []; % Remove the diagonal

Roger Stafford

Tags for this Thread

No tags are associated with 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