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:
vectorizing code

Subject: vectorizing code

From: Lex

Date: 28 Jun, 2010 10:05:13

Message: 1 of 5

Dear all,

how can I vectorize this pice of code? Let`s say m=100.
Thanks for your help.

[m n] = size(x);
y = sort(x);
for i=1:m
    for j=1:m
        ecop(i,j) = sum( (x(:,1)<=y(i,1)).*(x(:,2)<=y (j,2)) )/m;
    end
end

Subject: vectorizing code

From: Miroslav Balda

Date: 28 Jun, 2010 15:05:26

Message: 2 of 5

Lex <laux5101@gmx.net> wrote in message <1856195661.15616.1277719543151.JavaMail.root@gallium.mathforum.org>...
> Dear all,
>
> how can I vectorize this pice of code? Let`s say m=100.
> Thanks for your help.
>
> [m n] = size(x);
> y = sort(x);
> for i=1:m
> for j=1:m
> ecop(i,j) = sum( (x(:,1)<=y(i,1)).*(x(:,2)<=y (j,2)) )/m;
> end
> end

Hi Lex,

I am not sure if your code can be vectorized, nevertheless, it could be accelerated by simple modifications. The following code contains a slightly modified original, that improves behavior of the code by pre-definition of ecop matrix, and its improvement in the second half:

% Lex.m 2010-06-28
%%%%%%%%
m = inp('m',500,'%5d'); % FEX Id: 9033
x = rand(m,2);
y = sort(x);
ecop = zeros(m);
e = zeros(m);


tic
for i=1:m
    for j=1:m
        ecop(i,j) = sum( (x(:,1)<=y(i,1)).*(x(:,2)<=y (j,2)) );
    end
end
ecop = ecop/m;
toc

tic
for i=1:m
    row = single(x(:,1)<=y(i,1))';
    for j=1:m
        e(i,j) = row * single(x(:,2)<=y(j,2));
    end
end
e = e/m;
toc

normdel = norm(ecop-e)

The computing times of the second part are about one half of the first ones.
Best regards,

Mira

Subject: vectorizing code

From: Matt Fig

Date: 28 Jun, 2010 16:01:28

Message: 3 of 5

m = length(x);
y = sort(x);
AA = bsxfun(@le,x(:,1),y(:,1)');
BB = bsxfun(@le,x(:,2),y(:,2)');
ecop = double(AA')*double(BB)/m;

Subject: vectorizing code

From: Bruno Luong

Date: 28 Jun, 2010 16:27:08

Message: 4 of 5

Here is a fast one (using the following file from FEX)
http://www.mathworks.com/matlabcentral/fileexchange/23897-n-dimensional-histogram

% Data
x=ceil(50*rand(1000,2));

% OP's for-loop engine
tic
[m n] = size(x);
y = sort(x);
ecop1 = zeros(m);
for i=1:m
    for j=1:m
        ecop(i,j) = sum( (x(:,1)<=y(i,1)).*(x(:,2)<=y (j,2)) )/m;
    end
end
toc % 29.341238 seconds.

% Matt's engine
tic
[m n] = size(x);
y = sort(x);
AA = bsxfun(@le,x(:,1),y(:,1)');
BB = bsxfun(@le,x(:,2),y(:,2)');
ecop2 = double(AA')*double(BB)/m;
toc % 0.309713 seconds.

% Bruno's HISTCN engine
tic
[m n] = size(x);
y = sort(x);
[e1 I1 J1]=unique(y(:,1),'last');
[e2 I2 J2]=unique(y(:,2),'last');
e = cumsum(cumsum(histcn(x, e1, e2),1),2);
ecop3 = e(J1,J2)/m;
toc % Elapsed time is 0.036968 seconds.

% Bruno

Subject: vectorizing code

From: Lex

Date: 30 Jun, 2010 09:15:33

Message: 5 of 5

Dear all,

thank you very much for your suggestions. This helped me a lot.

Kind regards, Pat

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