How Do I make this plot faster?

2 visualizaciones (últimos 30 días)
Zeyuan
Zeyuan el 6 de Oct. de 2025
Editada: Walter Roberson el 7 de Oct. de 2025
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?

Respuestas (1)

Walter Roberson
Walter Roberson el 7 de Oct. de 2025
Editada: Walter Roberson el 7 de Oct. de 2025
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
Elapsed time is 0.005135 seconds.
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
Elapsed time is 0.002060 seconds.
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
Elapsed time is 0.002936 seconds.
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
Elapsed time is 0.001035 seconds.
whos t_x y
Name Size Bytes Class Attributes t_x 51x1 408 double y 51x10001 4080408 double
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
Elapsed time is 20.778554 seconds.

Categorías

Más información sobre Annotations en Help Center y File Exchange.

Productos


Versión

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by