Code optimization by way of selective computations
Show older comments
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.
Accepted Answer
More Answers (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
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!