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:
indexing computations based on row numbers involved

Subject: indexing computations based on row numbers involved

From: Kate

Date: 13 Oct, 2010 03:35:05

Message: 1 of 6

Hello,

Below I have some code that does computations based on rows of the matrix 'Coords'.
I'd like some help in creating an output matrix that indexes the answer. So , below, my answer given straight from the code is:

K =

        1877
        2272
        -886
        1087
       -1567
        1271

Now each of row of K is computed by combinations of pairs rows in the matrix 'Coords'.
The numbers of the pairs is what I'd like to get . So for K , it would be:

rows_used_ from_Coords_matrix_in_computing_values_for_K =
                                                                                          [ 1 2;
                                                                                                 1 3;
                                                                                                 2 1;
                                                                                                 2 3;
                                                                                                 3 1;
                                                                                                 3 2 ]

Can anyone lend some help in deriving the matrix "rows_used_ from_Coords_matrix_in_computing_values_for_K" .

thanks
Kate




|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clc;clear all;


Coords =
                [ 40 25 7 44
                       71 34 76 96
                       53 65 4 89 ]


 x1 =Coords(:,1)
  y1 = Coords(:,2)
  x2=Coords(:,3)
  y2= Coords(:,4)
  
  
  %%% d1 --> P1,L2 %%%
  
  [m n] =size(Coords)

K = zeros(m);

for k=1:m
    for j=1:m
        K(k,j) = (Coords(k,2)-Coords(k,4))*Coords(j,1) + (Coords(k,3)-Coords(k,1))*Coords(j,2) + ((Coords(k,1)*Coords(k,4)) - (Coords(k,3)*Coords(k,2)))

    end
end


K=K(K~=0)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Subject: indexing computations based on row numbers involved

From: Roger Stafford

Date: 13 Oct, 2010 05:08:03

Message: 2 of 6

"Kate " <chino_tones@hotmail.com> wrote in message <i939d9$9vc$1@fred.mathworks.com>...
> Below I have some code that does computations based on rows of the matrix ....
- - - - - - - - - -
  Make one small adjustment. In the nested for-loops write:

for k=1:m
    for j=[1:k-1,k+1:m]
        K(k,j) = {Coords stuff}
    end
end

Then you won't need the K=K(K~=0) line. For other values in 'Coords' that line might erase some rows you don't want erased.

Roger Stafford

Subject: indexing computations based on row numbers involved

From: Roger Stafford

Date: 13 Oct, 2010 05:27:03

Message: 3 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i93erj$238$1@fred.mathworks.com>...
> ......
> for k=1:m
> for j=[1:k-1,k+1:m]
> K(k,j) = {Coords stuff}
> end
> end
> .......
- - - - - - - - -
  No I didn't get that right. Do this:

K = zeros(m*(m-1),1);
p = 0;
for k=1:m
    for j=[1:k-1,k+1:m]
        p = p + 1;
        K(p) = {Coords stuff}
    end
end

Roger Stafford

Subject: indexing computations based on row numbers involved

From: Roger Stafford

Date: 13 Oct, 2010 07:28:03

Message: 4 of 6

"Kate " <chino_tones@hotmail.com> wrote in message <i939d9$9vc$1@fred.mathworks.com>...
> .......
> Below I have some code that does computations based on rows of the matrix 'Coords'.
> I'd like some help in creating an output matrix that indexes the answer. So , below, my answer given straight from the code is:
> .......
- - - - - - - - - -
  You can do away with your for-loops altogether in the following vectorized version:

m = size(C,1);
k = reshape(repmat(1:m,m-1,1),1,[]);
j = mod(floor(1+1/(2*m):1+1/m:m^2-1),m)+1;
K = (C(k,2)-C(k,4)).*C(j,1) + (C(k,3)-C(k,1)).*C(j,2) ...
    + ((C(k,1).*C(k,4)) - (C(k,3).*C(k,2)));

where 'C' is short for 'Coords'.

Roger Stafford

Subject: indexing computations based on row numbers involved

From: Roger Stafford

Date: 13 Oct, 2010 10:09:03

Message: 5 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i93n23$esg$1@fred.mathworks.com>...
> .......
> m = size(C,1);
> k = reshape(repmat(1:m,m-1,1),1,[]);
> j = mod(floor(1+1/(2*m):1+1/m:m^2-1),m)+1;
> K = (C(k,2)-C(k,4)).*C(j,1) + (C(k,3)-C(k,1)).*C(j,2) ...
> + ((C(k,1).*C(k,4)) - (C(k,3).*C(k,2)));
> .......
- - - - - - - - - -
  Here's another way without the complicated truncated fractions and 'mod':

m = size(C,1);
k = repmat(1:m,m,1);
j = k.';
t = eye(m)~=0;
k(t) = []; j(t) = [];
K = (C(k,2)-C(k,4)).*C(j,1) + (C(k,3)-C(k,1)).*C(j,2) ...
    + ((C(k,1).*C(k,4)) - (C(k,3).*C(k,2)));

Roger Stafford

Subject: indexing computations based on row numbers involved

From: Jan Simon

Date: 13 Oct, 2010 11:31:03

Message: 6 of 6

Dear Kate,

> I'd like some help in creating an output matrix that indexes the answer.
> ...
> The numbers of the pairs is what I'd like to get .
> ...
> rows_used_ from_Coords_matrix_in_computing_values_for_K =
> [ 1 2;
> 1 3;
> 2 1;
> 2 3;
> 3 1;
> 3 2 ]
>
> Can anyone lend some help in deriving the matrix "rows_used_ from_Coords_matrix_in_computing_values_for_K" .

So what is exactly your question?
You've presented a code, which solves the first question. Any problems with it?
Do you want the number of pairs? N(rows) * (N(rows) - 1)
Do you want the indices of the pairs? Use VChooseKO:
  http://www.mathworks.com/matlabcentral/fileexchange/26397
  rows_used_etc = VChooseKO(1:n_rows, 2);
Or Matt Figs COMBINATOR:
  http://www.mathworks.com/matlabcentral/fileexchange/24325

Kind regards, Jan

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