Vectors must be the same length

3 次查看(过去 30 天)
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