Vectors must be the same length

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)|');

Réponses (2)

Walter Roberson
Walter Roberson le 19 Fév 2023
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 commentaires

stepseq is not part of MATLAB, but possibly you copied it from a source such as https://www.mathworks.com/matlabcentral/answers/819500-how-do-i-multiply-sin-and-stepseq-functions#answer_689890
%Defining variables and limits
n = -40:40
n = 1×81
-40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11
w=[-50,50]
w = 1×2
-50 50
nlower=n(1)
nlower = -40
nupper=n(end)
nupper = 40
% 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))
x = 1×81
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
% Applying DTFT
if -50<= n <=50,
X = dtft(x,n,w);
whos n x X
plot(n,X,'LineWidth', 2)
grid on
grid minor
set(gca,'Fontsize', 8);
xlabel('\omega / \pi');
ylabel('|X(n)|');
end
Name Size Bytes Class Attributes X 2x1 32 double complex n 1x81 648 double x 1x81 648 double
Error using plot
Vectors must be the same length.
function [x,n] = stepseq(n0,n1,n2) % Copied From: <https://www.mathworks.com/matlabcentral/answers/12834-unsolved-function?s_tid=srchtitle#>
% Generates x(n) = u(n-n0); n1 <= n,n0 <= n2
% ------------------------------------------
% [x,n] = stepseq(n0,n1,n2)
%
if ((n0 < n1) | (n0 > n2) | (n1 > n2))
error('arguments must satisfy n1 <= n0 <= n2')
end
n = [n1:n2];
%x = [zeros(1,(n0-n1)), ones(1,(n2-n0+1))];
x = [(n-n0) >= 0];
end
function [X] = dtft(x,n,w)
% Computes Discrete-time Fourier Transform
% [X] = dtft(x,n,w)
% X = DTFT values computed at w frequencies
% x = finite duration sequence over n
% n = sample position vector
% w = frequency location vector
X = exp(-1i*w'*n) * x.';
% X = x*exp(-j*n'*w);
end
What went wrong? Well, you passed in only two values for the frequency location vector, so the output is going to be length 2.
The output of dtft is not the same size as the finite x inputs or the sample positions: it is the same size as the frequency vector. You should be plotting w against X, not n against X. And you will probably want more frequencies in your list.
Gizem Karslioglu
Gizem Karslioglu le 19 Fév 2023
Thank you for the answer! Yes, I have created step function to write u(n). I have played it with different number of both n and w without any success. Can we be able to match number of all variables? I have seen that 'linspace' could be an option.
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 le 19 Fév 2023
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 .

Connectez-vous pour commenter.

Chawa Mozy
Chawa Mozy le 19 Fév 2023

0 votes

% 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');

Produits

Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by