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:
Vectorization For loop

Subject: Vectorization For loop

From: Anish Goorah

Date: 11 Sep, 2007 19:02:21

Message: 1 of 6

I want to eliminate the for loop for something like this:

T = 100;
K = 5;
X = rand(T,K);

D = zeros(K,K);
for i=1:K
    for j=1:K
        D(i,j) = mean((X(:,i) - mean(X(:,i))).*(X(:,j) - ...
                 mean(X(:,j))));
    end
end
 
Any ideas? Thanks
                 

Subject: Vectorization For loop

From: Anish Goorah

Date: 11 Sep, 2007 19:10:13

Message: 2 of 6

The loop part isn't very clear in the previous post...it
should be something like

for i=1:K
    for j=1:K
        D(i,j) = mean((X(:,i) - mean(X(:,i))).*(X(:,j) - ...
                 mean(X(:,j))));
    end
end

Subject: Vectorization For loop

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 11 Sep, 2007 19:26:28

Message: 3 of 6

In article <fc6ons$6p$1@fred.mathworks.com>, "Anish Goorah"
<anish.goorah@mathworks.com> wrote:

> I want to eliminate the for loop for something like this:
>
> T = 100;
> K = 5;
> X = rand(T,K);
>
> D = zeros(K,K);
> for i=1:K
> for j=1:K
> D(i,j) = mean((X(:,i) - mean(X(:,i))).*(X(:,j) - ...
> mean(X(:,j))));
> end
> end
>
> Any ideas? Thanks
--------------------------
Try this:

 D = X-repmat(mean(X),T,1);
 D = D.'*D/T;

Roger Stafford

Subject: Vectorization For loop

From: Anish Goorah

Date: 11 Sep, 2007 19:55:24

Message: 4 of 6

ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)
wrote in message <ellieandrogerxyzzy-
1109071226280001@dialup-
4.232.60.182.dial1.losangeles1.level3.net>...
> In article <fc6ons$6p$1@fred.mathworks.com>, "Anish
Goorah"
> <anish.goorah@mathworks.com> wrote:
>
> > I want to eliminate the for loop for something like
this:
> >
> > T = 100;
> > K = 5;
> > X = rand(T,K);
> >
> > D = zeros(K,K);
> > for i=1:K
> > for j=1:K
> > D(i,j) = mean((X(:,i) - mean(X(:,i))).*(X(:,j) -
 ...
> > mean(X(:,j))));
> > end
> > end
> >
> > Any ideas? Thanks
> --------------------------
> Try this:
>
> D = X-repmat(mean(X),T,1);
> D = D.'*D/T;
>
> Roger Stafford


Beauty. Thanks

Subject: Vectorization For loop

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 11 Sep, 2007 19:58:28

Message: 5 of 6

In article <fc6rrc$a2$1@fred.mathworks.com>, "Anish Goorah"
<anish.goorah@mathworks.com> wrote:

> ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)
> wrote in message <ellieandrogerxyzzy-
> 1109071226280001@dialup-
> 4.232.60.182.dial1.losangeles1.level3.net>...
> > In article <fc6ons$6p$1@fred.mathworks.com>, "Anish
> Goorah"
> > <anish.goorah@mathworks.com> wrote:
> >
> > > I want to eliminate the for loop for something like
> this:
> > >
> > > T = 100;
> > > K = 5;
> > > X = rand(T,K);
> > >
> > > D = zeros(K,K);
> > > for i=1:K
> > > for j=1:K
> > > D(i,j) = mean((X(:,i) - mean(X(:,i))).*(X(:,j) -
> ...
> > > mean(X(:,j))));
> > > end
> > > end
> > >
> > > Any ideas? Thanks
> > --------------------------
> > Try this:
> >
> > D = X-repmat(mean(X),T,1);
> > D = D.'*D/T;
> >
> > Roger Stafford
>
> Beauty. Thanks
-------------
  I should also mention that you can accomplish the same thing with:

 D = cov(X,1);

Roger Stafford

Subject: Vectorization For loop

From: Jerome Briot

Date: 11 Sep, 2007 20:03:52

Message: 6 of 6

Roger Stafford a écrit :

> D = X-repmat(mean(X),T,1);
> D = D.'*D/T;

Hi,

or :

D=bsxfun(@minus,X,mean(X));
D = D.'*D/T;

Jérôme

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