Is it possible to use an array in an if statement?

Hi, I need to compare each member of an array against a fixed value and then modify that memeber according to whether it is smaller or larger than the fixed value. I can loop through the array to do this, but I'm trying to speed the code up and was wondering whether you could simply operate on the array directly? I've tried it and I get the same operation on each member irrespective of whether it is smaller or larger than the fixed value.
The code I've used is as follows:
if Gr<10^5
Nu_G=0.5.*Gr.^0.25;
else
Nu_G=0.13.*Gr.^0.33;
end
Can anyone advise whether this is possible or whether there may be any other way to achieve the same effect i.e. avoid the loop and/or speed up the code
Thanks,
Rebecca

 Respuesta aceptada

Daniel Shub
Daniel Shub el 8 de Nov. de 2011
Nu_G = zeros(size(Gr));
Nu_G(Gr<10^5) = 0.5.*Gr(Gr<10^5).^0.25;
Nu_G(Gr>=10^5) = 0.13.*Gr(Gr>=10^5).^0.33;
This is potentially slower than the loop method. In your if/else you only need to test Gr<10^5 once for every element, in the vectorized version you need to test twice.

Más respuestas (1)

Jakob Sievers
Jakob Sievers el 8 de Nov. de 2011
Assuming your GR vector is LGR long, then perhaps something like:
Nu_G=zeros(LGR,1);
Nu_g(Gr<10^5)=0.5.*Gr(Gr<10^5).^25;
Nu_g(Gr>=10^5)=0.13.*Gr(Gr>=10^5).^33;
There's probably even faster ways but this would definitely be faster than doing loops.

5 comentarios

Jakob Sievers
Jakob Sievers el 8 de Nov. de 2011
Ah, I see that Daniel was a few seconds faster than me.
Rebecca Ward
Rebecca Ward el 8 de Nov. de 2011
Fantastic response, thanks guys!
That's really helpful, didn't realise you could put the comparative statement like that.
Daniel Shub
Daniel Shub el 8 de Nov. de 2011
I am not sure this is always going to be faster than the loop. I think this solution requires the creation of a temporary logical variable of the size of Gr. The for loop solution doesn't. MATLAB loops are not particularly slow anymore (sometimes loops are in fact faster than vectorization).
Daniel Shub
Daniel Shub el 8 de Nov. de 2011
On my machine:
Gr = randn(1e8, 1);
tic
LGR = length(Gr);
Nu_G=zeros(LGR,1);
Nu_g(Gr<10^5)=0.5.*Gr(Gr<10^5).^25;
Nu_g(Gr>=10^5)=0.13.*Gr(Gr>=10^5).^33;
toc
tic
LGR = length(Gr);
Nu_G=zeros(LGR,1);
for iGr = 1:LGR
if Gr(iGr)<10^5
Nu_g(iGr)=0.5.*Gr(iGr).^25;
else
Nu_g(iGr)=0.13.*Gr(iGr).^33;
end
end
toc
Elapsed time is 104.696162 seconds.
Elapsed time is 41.326568 seconds.
For smaller N, the vectorization is faster.
Rebecca Ward
Rebecca Ward el 8 de Nov. de 2011
I found another way of doing this:
comp_Gr=Gr>10^5
Nu_G=(1-comp_Gr).*(0.5.*Gr.^0.25)+comp_Gr.*(0.13.*Gr.^0.33)
Don't know whether this is faster or slower?
Thanks for your help,
Rebecca

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by