How Do I make this plot faster?
조회 수: 16 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Walter Roberson
2025년 10월 7일 0:27
편집: Walter Roberson
2025년 10월 7일 0:47
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!