How Do I make this plot faster?
14 views (last 30 days)
Show older comments
I am trying to make this plot, but it is taking too much time. How do I make this plot faster?
Fs = 2500; % Hz
t = 0:1/Fs:4; % a vector that represents 1 second, sampled at Fs
indices = [1, 0.001*Fs+1, 0.002*Fs+1, 0.004*Fs+1, 0.008*Fs+1, 0.01*Fs+1];
indices = round(indices); % ensure integer indices
y = zeros(0.02*2500+1, 10001);
% This makes the y with only needed indices.
for k = 1:length(indices)
y(indices(k), indices(k)) = 1; % single "1" at the right position
end
%0.001 is not existing in the t array, so we make it rounded up so that we
%can print it out.
t_x = t(1:0.02*2500+1);
% This is for the t array
figure;
stem(t_x,y);
xlabel('time');
ylabel('vectors');
legend('e1','e4','e6','e11','e21','e26')
grid on;
axis normal;
% This is for printing out the unit vectors.
Also, when I plot out, the legend does not seems to match. Here is the plot
Aother question is that why t_x seems to be 1*51 and y seems to be 51*10001. Why does this works but when I use t_x and y', it does not work?

0 Comments
Answers (1)
Walter Roberson
31 minutes ago
Edited: Walter Roberson
11 minutes ago
You are plotting a 51 x 10001 array. That is slow.
Most of what is being plotted is all zeros. There is not a lot of point in plotting the all-zero entries (at least not more than one of them.)
tic
Fs = 2500; % Hz
t = 0:1/Fs:4; % a vector that represents 1 second, sampled at Fs
toc
tic
indices = [1, 0.001*Fs+1, 0.002*Fs+1, 0.004*Fs+1, 0.008*Fs+1, 0.01*Fs+1];
indices = round(indices); % ensure integer indices
toc
tic
y = zeros(0.02*2500+1, 10001);
% This makes the y with only needed indices.
for k = 1:length(indices)
y(indices(k), indices(k)) = 1; % single "1" at the right position
end
toc
tic
%0.001 is not existing in the t array, so we make it rounded up so that we
%can print it out.
t_x = t(1:0.02*2500+1)';
toc
whos t_x y
tic
% This is for the t array
figure;
stem(t_x,y);
xlabel('time');
ylabel('vectors');
legend('e1','e4','e6','e11','e21','e26')
grid on;
axis normal;
% This is for printing out the unit vectors.
toc
0 Comments
See Also
Categories
Find more on Annotations 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!