Vectors must be the same length.

1,065 次查看(过去 30 天)
Every time I run this code, I get "Error using plot Vectors must be the same length.
Error in ex (line 24) plot(t,abs(Y)) "
Here is my code:
clear
dt = 1/100000;
tstart = 0;
tend = 0.020;
t=[tstart : dt : tend];
xc=25*sin(2*pi*150*t)-15*cos(2*pi*800*t);
T = 0.00025;
n = [tstart/T : tend/T];
x1=25*sin(2*pi*150*n*T)-15*cos(2*pi*800*n*T);
% Quantize Signal
x2=fix(x1);
% Define filter coefficents
A = [1];
B = [1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16];
% Filter the quantize signal
Y = filter(B,A,x2);
figure(1)
subplot(211)
plot(t,abs(Y))
xlabel('Frequency (rad/sec)')
ylabel('Magntidue')
subplot(212)
plot(t,angle(Y))
xlabel('Frequency (rad/sec)')
ylabel('Phase')

回答(2 个)

the cyclist
the cyclist 2015-9-23
The proximate cause of your problem is that you are trying to make an X-Y plot, where X (your variable "t") has 2001 elements, and Y has 81 elements. So, MATLAB cannot figure out how plot each (X,Y) pair, because the two vectors do not have an equal number of points.
It is not easy for me to figure out what you intended to do.
  2 个评论
Nasser Aljadeed
Nasser Aljadeed 2015-9-23
How can I make these vectors have the same dimensions?
So : t =[tstart : dt : tend] has to have the same dimensions as n = [tstart/T : tend/T]?
the cyclist
the cyclist 2015-9-24
编辑:the cyclist 2015-9-24
Probably the easiest way to ensure an equal number of elements, and uniform spacing, is to use the linspace command to create both vectors.

请先登录,再进行评论。


Jae Song
Jae Song 2015-9-23
编辑:Jae Song 2015-9-23
It looks like the variables xc and t have same size. So if you let x2 = fix(xc) instead of x2 = fix(x1). The plot should work. (I don't know that was your intention or not)
clear
dt = 1/100000;
tstart = 0;
tend = 0.020;
t=[tstart : dt : tend];
xc=25*sin(2*pi*150*t)-15*cos(2*pi*800*t);
T = 0.00025;
n = [tstart/T : tend/T];
x1=25*sin(2*pi*150*n*T)-15*cos(2*pi*800*n*T);
% Quantize Signal
x2=fix(xc);
% Define filter coefficents
A = [1];
B = [1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16 1/16];
% Filter the quantize signal
Y = filter(B,A,x2);
figure(1);
subplot(211);
plot(t,abs(Y))
xlabel('Frequency (rad/sec)')
ylabel('Magntidue')
subplot(212)
plot(t,angle(Y))
xlabel('Frequency (rad/sec)')
ylabel('Phase')
  2 个评论
saurabh jain
saurabh jain 2021-4-3
编辑:saurabh jain 2021-4-3
can u help me in this code
Image Analyst
Image Analyst 2021-4-3
编辑:Image Analyst 2021-4-3
@saurabh jain, probably not since @Jae Song was last here in 2015. But you might read this:
and then post your code (not an image of your code) in a new question so people can fix it.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Single-Rate Filters 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by