Vectors must be the same length.

1,054 views (last 30 days)
Nasser Aljadeed
Nasser Aljadeed on 23 Sep 2015
Edited: Image Analyst on 3 Apr 2021
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')

Answers (2)

the cyclist
the cyclist on 23 Sep 2015
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 Comments
Nasser Aljadeed
Nasser Aljadeed on 23 Sep 2015
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 on 24 Sep 2015
Edited: the cyclist on 24 Sep 2015
Probably the easiest way to ensure an equal number of elements, and uniform spacing, is to use the linspace command to create both vectors.

Sign in to comment.


Jae Song
Jae Song on 23 Sep 2015
Edited: Jae Song on 23 Sep 2015
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 Comments
saurabh jain
saurabh jain on 3 Apr 2021
Edited: saurabh jain on 3 Apr 2021
can u help me in this code
Image Analyst
Image Analyst on 3 Apr 2021
Edited: Image Analyst on 3 Apr 2021
@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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!