Vectors must be the same length

조회 수: 3 (최근 30일)
Gizem Karslioglu
Gizem Karslioglu 2023년 2월 19일
댓글: Gizem Karslioglu 2023년 2월 19일
I am receiving a warning ' Vectors must be the same length' for 'plot(n,X,'LineWidth', 2)' . I have tried linspace but it didn't work. How can I solve it?
%Defining variables and limits
n = -40:40
w=[-50,50]
nlower=n(1)
nupper=n(end)
% Writing the x signal function x(n) = n(0.9)^n [u(n)-u(n-21)]
subplot(3,1,1)
x = n .*(0.9) .^ n.* (stepseq(0,nlower,nupper)-stepseq(21,nlower,nupper))
% Applying DTFT
if -50<= n <=50,
X = dtft(x,n,w);
plot(n,X,'LineWidth', 2)
grid on
grid minor
set(gca,'Fontsize', 8);
xlabel('\omega / \pi');
ylabel('|X(n)|');

답변 (2개)

Walter Roberson
Walter Roberson 2023년 2월 19일
if -50<= n <=50,
MATLAB interprets that as
if ((-50<= n) <=50)
The first part compares every element in n to -50. You defined n as -40:40 so it is true that -50<=n for every entry in n. The result of that part of the expression is an array of logical true values the same size as n. That array of logical true values is then compared <= 50. logical true in mathematical contexts converts to 1 (false converts to 0) and for each of those true values from the first part of the expression, true <= 50 is true, so the result of the <= tests is going to be an array of true values the same size as n. Then when if is asked to test a non-scalar, the result is considered true only of all of the values being tested are non-zero. Which is the case because they are all true. So the if will pass, and might as well not have been there.
  댓글 수: 4
Walter Roberson
Walter Roberson 2023년 2월 19일
w = linspace(-50, 50, length(n));
However if you do this, you are likely to get confused between positions (n values) and frequencies (w values).
When you have M = length(n) input positions, you cannot justify more than M/2 frequencies as being fully resolved; any more than that you run into Nyquist limit problems. Even M/2 is probably an over-estimate.
Gizem Karslioglu
Gizem Karslioglu 2023년 2월 19일
Is there any way to make value of all variables matched? When I try to manually change n and w, r never gets the same value.
I ended up below plots if I plot(w,X). But, my goal is plotting (n,X) and I am expecting to see a sinusoidal signal .

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


Chawa Mozy
Chawa Mozy 2023년 2월 19일
% Define variables and limits n = -40:40; w = [-50, 50]; nlower = n(1); nupper = n(end);
% Define the signal x(n) x = n .* (0.9) .^ n .* (stepseq(0, nlower, nupper) - stepseq(21, nlower, nupper));
% Compute and plot the magnitude and phase spectra using the DTFT X = dtft(x, n, w);
subplot(3, 1, 1) plot(n, x, 'LineWidth', 2) grid on grid minor set(gca, 'Fontsize', 8); xlabel('n'); ylabel('x(n)'); title('Signal x(n)');
subplot(3, 1, 2) plot(w, abs(X), 'LineWidth', 2); grid on grid minor set(gca, 'Fontsize', 8); xlabel('\omega / \pi'); ylabel('|X(\omega)|'); title('Magnitude Spectrum');
subplot(3, 1, 3) plot(w, angle(X), 'LineWidth', 2); grid on grid minor set(gca, 'Fontsize', 8); xlabel('\omega / \pi'); ylabel('\angle X(\omega)'); title('Phase Spectrum');

카테고리

Help CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by