# Anonymous function or for loop?

4 visualizaciones (últimos 30 días)
Amanda Liu el 24 de Jun. de 2021
Editada: Amanda Liu el 24 de Jun. de 2021
Assume I have a simple electrical function of V=C*sin(t) where C is a constant which varies based on t intervals.
I have used 2 methods to generate the C and by using tic/toc to check, Method 2 seems to be faster than Method 1.
For efficiency and speed wise, should I always use anonymous function instead of for loop? Or is my loop not being coded efficient enough?
t = 0 : 0.1 : 20;
c1 = 53; % (0 <= t <= 10)
c2 = 68; % (10 < t <= 20)
% Method 1 (For Loop)
tic
n = length(t);
a = ones(1,n);
for i = 1:n
if t(i)>=0 && t(i)<=10
A = c1;
else
A = c2;
end
a(i) = A;
end
toc
% Method 2 (Anonymous Function)
tic
C = @(t) (t>=0 & t<=10).*c1 + (t>10).*c2;
c = C(t);
toc
v = sin(t);
v2 = c.*v;
v3 = a.*v;
% Checking
a==c;
v2==v3;
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

KSSV el 24 de Jun. de 2021
Off course, you can achieve the same without using anonymous function.
t = 0 : 0.1 : 20;
c1 = 53; % (0 <= t <= 10)
c2 = 68; % (10 < t <= 20)
tic
a = c1*ones(size(t)) ;
a(t>10) = c2 ;
toc
Elapsed time is 0.009947 seconds.
% Method 2 (Anonymous Function)
tic
C = @(t) (t>=0 & t<=10).*c1 + (t>10).*c2;
c = C(t);
toc
Elapsed time is 0.005566 seconds.
v = sin(t);
v3 = a.*v;
v4 = a.*v ;
% Checking
isequal(a,c)
ans = logical
1
isequal(v3,v4)
ans = logical
1
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Amanda Liu el 24 de Jun. de 2021
Ohh..thank you again! ^^

Iniciar sesión para comentar.

### Más respuestas (2)

KSSV el 24 de Jun. de 2021
Editada: KSSV el 24 de Jun. de 2021
The second version is a vectorised version and this will be fast compared to loop.
You check the finals values uisng:
% Checking
isequal(a,c)
isequal(v2,v3)
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Amanda Liu el 24 de Jun. de 2021
I see, thank you!

Iniciar sesión para comentar.

Sulaymon Eshkabilov el 24 de Jun. de 2021
Your second method is much faster. Try to avoid for or while loop if feasible.
At the same time, you may improve your 2nd method by computing the values of C directly for the predefined values of t, e.g:
% Method 2 (Anonymous Function)
t1 =0:0.1:10; t2 =10.01:.1:20; % t is split up into two ranges
tic
C =t1*c1 + t2*c2;
toc
v = sin(t);
v2 = C.*v;
v3 = a.*v;
% Checking
a==c;
v2==v3;
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Amanda Liu el 24 de Jun. de 2021
Editada: Amanda Liu el 24 de Jun. de 2021
Ohh..so I wouldn't need an anonymous function anymore.
Btw, I think you meant
t2 = 10.1 : .1 : 20 % instead of 10.01 : .1 : 20
I'm getting dimension error in this line:
C = t1*c1 + t2*c2 % (t1 is of size 1x101 while t2 is 1x100)
What I understand from the line above is that C is the sum of 2 scalar values meaning that even if I change t1 and t2 to be of the same size, there will still only be a total of 101 Cs' even though it should be 201 Cs'.
Therefore, I changed it into:
C = [t1*c1 t2*c2];
However if i check C & a using isequal(a,C), it returns false because t itself should not be multiplied into the constants.
Anyways, thank you very much for helping me! I will avoid for/while loop if feasible.

Iniciar sesión para comentar.

### Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by