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:
Avoiding loops in a symmetric matrix.

Subject: Avoiding loops in a symmetric matrix.

From: Jose

Date: 5 Dec, 2008 14:29:02

Message: 1 of 11

Hi,
I have 10 vectors with 10 components, u(1:10,1:10).

And I am interested to calculated the differences between vectors avoiding the diagonal components, because I am not interested in a high correlation between components.

This is my code, it works, but is not efficient, I would like to avoid the loops, but
I do not know how I can do it:

for i=1:10
   
    for j=1:10
        if j~=i
        dx=u(i,:)-u(j,:);
        
   if i==1 & j==2
   delta=dx;
   else
       delta=[delta;dx];
   end
        end
        end

Please, anyone can help me to try to built the delta correlation matrix of another form more efficient.
Thanks in advance,
Jose.

Subject: Avoiding loops in a symmetric matrix.

From: Matt

Date: 5 Dec, 2008 15:17:02

Message: 2 of 11

"Jose " <jose.l.vega@gmail.com> wrote in message <ghbdre$ahh$1@fred.mathworks.com>...
> Hi,
> I have 10 vectors with 10 components, u(1:10,1:10).
>
> And I am interested to calculated the differences between vectors avoiding the diagonal components, because I am not interested in a high correlation between components.
>
> This is my code, it works, but is not efficient, I would like to avoid the loops, but
> I do not know how I can do it:
>
> for i=1:10
>
> for j=1:10
> if j~=i
> dx=u(i,:)-u(j,:);
>
> if i==1 & j==2
> delta=dx;
> else
> delta=[delta;dx];
> end
> end
> end
>
> Please, anyone can help me to try to built the delta correlation matrix of another form more efficient.
> Thanks in advance,
> Jose.


You can eliminate one loop using circshift(). I don't see a way you can avoid both.

But why do you care? It's such a small amount of data.

Subject: Avoiding loops in a symmetric matrix.

From: Jose

Date: 5 Dec, 2008 15:33:02

Message: 3 of 11

Hi Matt, could you explain me how i can do it with circshift?
I need to make the differences of 200 vectors and 100 components,
it take 5 minutes, and -i have to get it in secs, :)
I wrote a short example, to explain my problem.
Cheers,
Jose.


"Matt" <mjacobson.removethis@xorantech.com> wrote in message <ghbgle$s08$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <ghbdre$ahh$1@fred.mathworks.com>...
> > Hi,
> > I have 10 vectors with 10 components, u(1:10,1:10).
> >
> > And I am interested to calculated the differences between vectors avoiding the diagonal components, because I am not interested in a high correlation between components.
> >
> > This is my code, it works, but is not efficient, I would like to avoid the loops, but
> > I do not know how I can do it:
> >
> > for i=1:10
> >
> > for j=1:10
> > if j~=i
> > dx=u(i,:)-u(j,:);
> >
> > if i==1 & j==2
> > delta=dx;
> > else
> > delta=[delta;dx];
> > end
> > end
> > end
> >
> > Please, anyone can help me to try to built the delta correlation matrix of another form more efficient.
> > Thanks in advance,
> > Jose.
>
>
> You can eliminate one loop using circshift(). I don't see a way you can avoid both.
>
> But why do you care? It's such a small amount of data.
>

Subject: Avoiding loops in a symmetric matrix.

From: Matt

Date: 5 Dec, 2008 16:36:01

Message: 4 of 11

"Jose " <jose.l.vega@gmail.com> wrote in message <ghbhje$e2j$1@fred.mathworks.com>...
> Hi Matt, could you explain me how i can do it with circshift?
> I need to make the differences of 200 vectors and 100 components,
> it take 5 minutes, and -i have to get it in secs, :)
> I wrote a short example, to explain my problem.
> Cheers,
> Jose.

Well, for example, if your original matrix is u and you want to subtract each column of u from its neighbour to the right, you do the following

delta1=u-circshift(u,[0,-1]);

From there, I think it's obvious how you do the remaining delta's.

A few more tips

1. Pre-allocate your delta array rather than letting it grow by concatentation.

2. It's faster to loop over matrix columns than rows. So, pre-tranpose your matrix to facilitate this.

Subject: Avoiding loops in a symmetric matrix.

From: Jose

Date: 5 Dec, 2008 19:07:01

Message: 5 of 11

clc;
clear all;
u=[1 2 3; 3 4 6;5 6 7 ]
delta1=u-circshift(u,[0,-1])

u =

     1 2 3
     3 4 6
     5 6 7


delta1 =

    -1 -1 2 ...first colum: u(:,1)-u(:,2)
    -1 -2 3....second column: u(:,2)-u(:,3)
    -1 -1 2....third column: (:,3)-u(:,1)

But...i am more interested in the sequence:
u(:,1)-u(:,2),u(:,1)-u(:,3)
And i do not know how you can arrange from your idea at my final matrix.
ome idea?
Cheers,
Jose.

Thanks for the tips.

Ok, you are right...but to me is not obvious :)...how I can generate the
remaind deltas.



"Jose " <jose.l.vega@gmail.com> wrote in message <ghbdre$ahh$1@fred.mathworks.com>...
> Hi,
> I have 10 vectors with 10 components, u(1:10,1:10).
>
> And I am interested to calculated the differences between vectors avoiding the diagonal components, because I am not interested in a high correlation between components.
>
> This is my code, it works, but is not efficient, I would like to avoid the loops, but
> I do not know how I can do it:
>
> for i=1:10
>
> for j=1:10
> if j~=i
> dx=u(i,:)-u(j,:);
>
> if i==1 & j==2
> delta=dx;
> else
> delta=[delta;dx];
> end
> end
> end
>
> Please, anyone can help me to try to built the delta correlation matrix of another form more efficient.
> Thanks in advance,
> Jose.

Subject: Avoiding loops in a symmetric matrix.

From: Matt

Date: 5 Dec, 2008 19:20:18

Message: 6 of 11

> Ok, you are right...but to me is not obvious :)...how I can generate the
> remaind deltas.

If it's not obvious, I'm pretty sure you haven't bothered to do "help circshift"
:)

Subject: Avoiding loops in a symmetric matrix.

From: Jose

Date: 5 Dec, 2008 19:51:02

Message: 7 of 11

Please, Matt, can yu written down the code complete for the matrix u?
I spent all the day try to vectorizing the matrix avoiding loops.
Here you are.
u=[1 2 3; 4 5 6; 6 7 8]

clear all;
clc;


u=[1 2 3; 4 5 6; 6 7 8]

tic
nc=size(u,2);

delta=zeros(0,nc); %pre-llocated delta

nt=length(u(:,1))
for i=1:nt
    for j=1:nt
    dx=u(i,:)-u(j,:);
    delta=[delta;dx];
    end
    end


delta
toc

     0 0 0
    -3 -3 -3
    -5 -5 -5
     3 3 3
     0 0 0
    -2 -2 -2
     5 5 5
     2 2 2
     0 0 0






You can send me your code, where you avoid the j-loop?

I do not need the diagonal.





Thanks in advance,
Jose.


"Jose " <jose.l.vega@gmail.com> wrote in message <ghbdre$ahh$1@fred.mathworks.com>...
> Hi,
> I have 10 vectors with 10 components, u(1:10,1:10).
>
> And I am interested to calculated the differences between vectors avoiding the diagonal components, because I am not interested in a high correlation between components.
>
> This is my code, it works, but is not efficient, I would like to avoid the loops, but
> I do not know how I can do it:
>
> for i=1:10
>
> for j=1:10
> if j~=i
> dx=u(i,:)-u(j,:);
>
> if i==1 & j==2
> delta=dx;
> else
> delta=[delta;dx];
> end
> end
> end
>
> Please, anyone can help me to try to built the delta correlation matrix of another form more efficient.
> Thanks in advance,
> Jose.

Subject: Avoiding loops in a symmetric matrix.

From: Matt

Date: 5 Dec, 2008 20:28:03

Message: 8 of 11

> You can send me your code, where you avoid the j-loop?
>
> I do not need the diagonal.

OK. Here's what I have. However, it produces an array DeltaUnsorted whose columns may not be in the order you want. That part, I leave to you...

u=rand(100,200);
nn=size(u,2);
Z=cell(1,nn-1);

tic;
 for ii=1:nn-1,
  Z{ii}=u-circshift(u,[0,-ii]);
 end,
DeltaUnsorted=[Z{:}];
toc

Elapsed time is 0.165232 seconds.

Subject: Avoiding loops in a symmetric matrix.

From: Jose

Date: 5 Dec, 2008 21:00:21

Message: 9 of 11

Thaks, thank you very much.
It is like i need, because i am not interested in the order, only in the differences.
Well done Matt,
Cheers,
Jose.

"Jose " <jose.l.vega@gmail.com> wrote in message <ghbdre$ahh$1@fred.mathworks.com>...
> Hi,
> I have 10 vectors with 10 components, u(1:10,1:10).
>
> And I am interested to calculated the differences between vectors avoiding the diagonal components, because I am not interested in a high correlation between components.
>
> This is my code, it works, but is not efficient, I would like to avoid the loops, but
> I do not know how I can do it:
>
> for i=1:10
>
> for j=1:10
> if j~=i
> dx=u(i,:)-u(j,:);
>
> if i==1 & j==2
> delta=dx;
> else
> delta=[delta;dx];
> end
> end
> end
>
> Please, anyone can help me to try to built the delta correlation matrix of another form more efficient.
> Thanks in advance,
> Jose.

Subject: Avoiding loops in a symmetric matrix.

From: Roger Stafford

Date: 5 Dec, 2008 21:46:01

Message: 10 of 11

"Jose " <jose.l.vega@gmail.com> wrote in message <ghbdre$ahh$1@fred.mathworks.com>...
> Hi,
> I have 10 vectors with 10 components, u(1:10,1:10).
>
> And I am interested to calculated the differences between vectors avoiding the diagonal components, because I am not interested in a high correlation between components.
>
> This is my code, it works, but is not efficient, I would like to avoid the loops, but
> I do not know how I can do it:
>
> for i=1:10
>
> for j=1:10
> if j~=i
> dx=u(i,:)-u(j,:);
>
> if i==1 & j==2
> delta=dx;
> else
> delta=[delta;dx];
> end
> end
> end
>
> Please, anyone can help me to try to built the delta correlation matrix of another form more efficient.
> Thanks in advance,
> Jose.

  You can also do it this way:

 n = size(u,1);
 [I,J] = meshgrid(1:n,1:n);
 p = 1+(n+1)*(0:n-1);
 I(p) = []; J(p) = [];
 delta2 = u(I,:)-u(J,:);

Roger Stafford

Subject: Avoiding loops in a symmetric matrix.

From: Jose

Date: 8 Dec, 2008 16:28:02

Message: 11 of 11

Thanks Roger.
Jose.

"Jose " <jose.l.vega@gmail.com> wrote in message <ghbdre$ahh$1@fred.mathworks.com>...
> Hi,
> I have 10 vectors with 10 components, u(1:10,1:10).
>
> And I am interested to calculated the differences between vectors avoiding the diagonal components, because I am not interested in a high correlation between components.
>
> This is my code, it works, but is not efficient, I would like to avoid the loops, but
> I do not know how I can do it:
>
> for i=1:10
>
> for j=1:10
> if j~=i
> dx=u(i,:)-u(j,:);
>
> if i==1 & j==2
> delta=dx;
> else
> delta=[delta;dx];
> end
> end
> end
>
> Please, anyone can help me to try to built the delta correlation matrix of another form more efficient.
> Thanks in advance,
> Jose.

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