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:
Accelaration of a for loop

Subject: Accelaration of a for loop

From: Ioannis Botonakis

Date: 12 Nov, 2009 09:44:02

Message: 1 of 6

Hallo to everyone,

I have been trying for a very long time the following code to accelerate but every effort has failed:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc,clear
aa = 2e4;
bb = aa/2;
Xf = rand(aa,1);
Yf = rand(aa,1);
Zf = rand(aa,1);

X_elem_nx = rand(bb,2);
Y_elem_nx = rand(bb,2);
Z_elem_nx = rand(bb,2);
tic
for ii = 1:length(Xf)
    wwA = find(Xf(ii)>X_elem_nx(:,1) & Xf(ii)<X_elem_nx(:,2) &...
                     Yf(ii)>Y_elem_nx(:,1) & Yf(ii)<Y_elem_nx(:,2) &...
                     Zf(ii)>Z_elem_nx(:,1) & Zf(ii)<Z_elem_nx(:,2));
end
toc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Can someone help? Is it possible to do the comparison without a for loop? I dont have the parallel Toolbox in order to use the parfor scenario.

Many thanks!

Jannis Botonakis

Subject: Accelaration of a for loop

From: Miroslav Balda

Date: 12 Nov, 2009 16:52:02

Message: 2 of 6

"Ioannis Botonakis" <i.botonakis_withoutthis_@googlemail.com> wrote in message <hdgld2$86j$1@fred.mathworks.com>...
> Hallo to everyone,
>
> I have been trying for a very long time the following code to accelerate but every effort has failed:
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> clc,clear
> aa = 2e4;
> bb = aa/2;
> Xf = rand(aa,1);
> Yf = rand(aa,1);
> Zf = rand(aa,1);
>
> X_elem_nx = rand(bb,2);
> Y_elem_nx = rand(bb,2);
> Z_elem_nx = rand(bb,2);
> tic
> for ii = 1:length(Xf)
> wwA = find(Xf(ii)>X_elem_nx(:,1) & Xf(ii)<X_elem_nx(:,2) &...
> Yf(ii)>Y_elem_nx(:,1) & Yf(ii)<Y_elem_nx(:,2) &...
> Zf(ii)>Z_elem_nx(:,1) & Zf(ii)<Z_elem_nx(:,2));
> end
> toc
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> Can someone help? Is it possible to do the comparison without a for loop? I dont have the parallel Toolbox in order to use the parfor scenario.
>
> Many thanks!
>
> Jannis Botonakis

Hi Jannis,
I think that the code you have presented is not that you really want to have. Why not? If you look carefully, you observe, that your variable wwA contains only results of the last loop, i.e. under ii = aa.
I have prepared the foolowing script, which is in the Code A identical with your code, Code B yields just the same results as Code A, however, in much shorter time. Code C probably solves your task correctly, however, in longer time than Code A.
Sorry that I give you no answer how to accelerate the code.
Good luck
Mira

% Janis.m 2009-11-12
clc,clear all
aa = 2e4;
bb = aa/2;
Xf = rand(aa,1);
Yf = rand(aa,1);
Zf = rand(aa,1);

X_elem_nx = rand(bb,2);
Y_elem_nx = rand(bb,2);
Z_elem_nx = rand(bb,2);
% Code A
tic
for ii = 1:length(Xf)
    wwA1 = find(Xf(ii)>X_elem_nx(:,1) & Xf(ii)<X_elem_nx(:,2) &...
                Yf(ii)>Y_elem_nx(:,1) & Yf(ii)<Y_elem_nx(:,2) &...
                Zf(ii)>Z_elem_nx(:,1) & Zf(ii)<Z_elem_nx(:,2));
end
size(wwA1)
toc
% Code B
tic
wwA2 = find(Xf(ii)>X_elem_nx(:,1) & Xf(ii)<X_elem_nx(:,2) &...
            Yf(ii)>Y_elem_nx(:,1) & Yf(ii)<Y_elem_nx(:,2) &...
            Zf(ii)>Z_elem_nx(:,1) & Zf(ii)<Z_elem_nx(:,2));
size(wwA2)
toc
% Code C
tic
L = false(bb,1);
for ii = 1:aa
    L = L |...
        Xf(ii)>X_elem_nx(:,1) & Xf(ii)<X_elem_nx(:,2) &...
        Yf(ii)>Y_elem_nx(:,1) & Yf(ii)<Y_elem_nx(:,2) &...
        Zf(ii)>Z_elem_nx(:,1) & Zf(ii)<Z_elem_nx(:,2);
end
wwA3 = int16(1:bb)';
wwA3 = wwA3(L);
size(wwA3)
toc
%--------------------- Results:
ans =
    54 1
Elapsed time is 9.142519 seconds.
ans =
    54 1
Elapsed time is 0.000516 seconds.
ans =
        1247 1
Elapsed time is 9.314007 seconds.

Subject: Accelaration of a for loop

From: Ioannis Botonakis

Date: 13 Nov, 2009 09:00:20

Message: 3 of 6

Hallo Miroslav,

thanks a lot for your reply. You are absolutely right, but in this example I just wrote the part of the code, which is terribly slow. The "for"-loop is necessary and the code looks like this:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc,clear
aa = 2e4;
bb = aa/2;
Xf = rand(aa,1);
Yf = rand(aa,1);
Zf = rand(aa,1);

X_elem_nx = rand(bb,2);
Y_elem_nx = rand(bb,2);
Z_elem_nx = rand(bb,2);

L = zeros(length(Xf),1);
tic
for ii = 1:length(Xf)
    wwA = find(Xf(ii)>X_elem_nx(:,1) & Xf(ii)<X_elem_nx(:,2) &...
                     Yf(ii)>Y_elem_nx(:,1) & Yf(ii)<Y_elem_nx(:,2) &...
                     Zf(ii)>Z_elem_nx(:,1) & Zf(ii)<Z_elem_nx(:,2));
L(ii)=wwA; % in the actual code, wwA is only a scalar
end
toc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Any more Ideas??

Sincerely
Yannis

Subject: Accelaration of a for loop

From: Darren Rowland

Date: 13 Nov, 2009 09:19:05

Message: 4 of 6

Yannis,
Do your actual X_elem_nx vectors have any structure to them or are they random?
By structure I mean e.g.are they in sorted order?
What is the problem you are trying to solve?

Darren

Subject: Accelaration of a for loop

From: Jan Simon

Date: 13 Nov, 2009 09:46:04

Message: 5 of 6

Dear Ioannis!

> for ii = 1:length(Xf)
> wwA = find(Xf(ii)>X_elem_nx(:,1) & Xf(ii)<X_elem_nx(:,2) &...
> Yf(ii)>Y_elem_nx(:,1) & Yf(ii)<Y_elem_nx(:,2) &...
> Zf(ii)>Z_elem_nx(:,1) & Zf(ii)<Z_elem_nx(:,2));
> L(ii)=wwA; % in the actual code, wwA is only a scalar
> end

No solution, but an acceleration at least:
Evaluating the indices takes time, so omit repeated indexing:

  X_elem_nx1 = X_elem_nx(:,1);
  X_elem_nx2 = X_elem_nx(:,2);
  Y_elem_nx1 = Y_elem_nx(:,1);
  Y_elem_nx2 = Y_elem_nx(:,2);
  Z_elem_nx1 = Z_elem_nx(:,1);
  Z_elem_nx2 = Z_elem_nx(:,2);

 for ii = 1:length(Xf)
     wwA = find(Xf(ii)>X_elem_nx1 & Xf(ii)<X_elem_nx2 &...
                      Yf(ii)>Y_elem_nx1 & Yf(ii)<Y_elem_nx2 &...
                      Zf(ii)>Z_elem_nx1 & Zf(ii)<Z_elem_nx2);
     L(ii)=wwA;
 end

Good luck, Jan

Subject: Accelaration of a for loop

From: Ioannis Botonakis

Date: 13 Nov, 2009 10:16:03

Message: 6 of 6

"Darren Rowland" <darrenjremovethisrowland@hotmail.com> wrote in message <hdj8a9$nup$1@fred.mathworks.com>...
> Yannis,
> Do your actual X_elem_nx vectors have any structure to them or are they random?
> By structure I mean e.g.are they in sorted order?
> What is the problem you are trying to solve?
>
> Darren

Hallo Darren,

surely are not sorted and I can say that they have a random structure. I'm programming 3D finite elements and in this case hexaedra. Im trying to determine if a point in space (with coordinates Xf,Yf,Zf) exists in a hexaedra or outside of it. In that way I can determine the different properties of each hexaedra. Thats why the comparison in all the x-y-z Coordinates is necessary.

Yannis

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