# When I executed below code it got executed perfectly but in the graph I am getting just the coordinates but not the plot.

조회 수: 4 (최근 30일)
keerthana reddy 2021년 6월 4일
댓글: keerthana reddy 2021년 6월 9일
f=10; %frequency of the impulse in Hz
fs=f*100; % sample frequency is 10 times higher
t=-1:1/fs:1; % time vector
x= 100*cos(2*pi*1000*t);
y=zeros(size(t));
y(1:fs/f:end)=1;
for t= -1:1/fs:1
m = x.*y;
end
plot(t,m);

댓글을 달려면 로그인하십시오.

### 채택된 답변

Walter Roberson 2021년 6월 7일
f=10; %frequency of the impulse in Hz
fs=f*100; % sample frequency is 10 times higher
t=-1:1/fs:1; % time vector
numt = length(t);
y=zeros(size(t));
y(1:fs/f:end)=1;
for tidx = 1 : numt
x = 100*cos(2*pi*1000*t(tidx));
m(tidx, :) = x.*y;
end
plot(t,m);
##### 댓글 수: 7이전 댓글 5개 표시이전 댓글 5개 숨기기
Walter Roberson 2021년 6월 7일
f = 10; %frequency of the impulse in Hz
fs = f * 7; % sample frequency is 7 times higher
t =-1/10:1/fs:1/10; % time vector
y = 100*cos(2*pi*1000*t);
stem(t, y)
Notice that we reduced the sampling frequency and the time interval.
Notice too that no loop was needed.
keerthana reddy 2021년 6월 9일
Wow!! This is great.Thank you for helping.

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Based on the above code it is clear that there is no use of the for loop by redefining the variable t to iterate over the loop and the value of m = x.*y; will be same for all the iterations. I think what you are looking for is may be the following:
f=10; %frequency of the impulse in Hz
fs=f*100; % sample frequency is 10 times higher
t=-1:1/fs:1; % time vector
x= 100*cos(2*pi*1000*t);
y=zeros(size(t));
y(1:fs/f:end)=1;
% Removing the for loop
m = x.*y;
plot(t,m);
##### 댓글 수: 4이전 댓글 2개 표시이전 댓글 2개 숨기기
keerthana reddy 2021년 6월 7일
Yeah, I just checked putting breakpoints. But, can you tell me how can I get different x at different t. But y value should be constant and when I execute m = x.*y I need to get discrete values.
편집: Srivardhan Gadila 2021년 6월 7일
If your equation is m(tidx) = x(tidx) * y(tidx) then the above code in my answer should work fine because with the operator times, .* element wise multiplication is performed so you don't have to iterate over individual elements of the vectors x and y, refer to the documentation of times, .* for more information. If your equation is m(tidx) = x(tidx) * y then you can refer to the below code posted by Walter.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by