Code optimization by way of selective computations
Mostrar comentarios más antiguos
I have the following code:
clc; clearvars;
Ts = 1e-1; t = 0:Ts:1-Ts
flag = mod(1:length(t),2)
s_t = exp(1i*2*pi*t)
I am looking to only execute the
computation only when the corresponding value for
is logical 1. I wish to do this without iterating as the final vector to process is
long and is about 97% zero values in the result.
Respuesta aceptada
Más respuestas (1)
Ts = 1e-7; t = 0:Ts:1-Ts;
tic
s_t = exp(1i*2*pi*t(1:2:end));
toc
or if your condition is more complicated:
tic
s_t = exp(1i*2*pi*t(mod(1:length(t),2)==1));
toc
Categorías
Más información sobre Operating on Diagonal Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!