Vectors must be the same length error
Show older comments
Hello everyone, im facing a problem that i cant solve it. Im new to MatLab and im having a , Vectors must be the same length, error. I dont know that well about grafics in matlab so if someone could help me it would be apreciated
heres my code
bny = input('Insira o codigo binario a codificar: ' ,'s');
idx = ismember(bny,'01');
assert(all(idx),'O codigo binario só pode conter 0s e 1s, mas contem o(s) número(s) %s',bny(~idx))
fprintf('O seu codigo é: %s\n',bny)
V = [-3,3];
n = V(bny-'/');
i=1;
a=0;
b=0.5;
t=0:0.01:length(bny);
for j=1:length(bny)
if t(j)>=a && t(j)<=b
y(j)=V(i);
elseif t(j)>b && t(j)<=i
y(j)=0;
else
i= i+1;
a=a+1;
b=b+1;
end
end
plot(t,y,'k');axis([0 length(bny) -5 5]);title('Rz Polar');
xlabel('time-->');
ylabel('Amplitude-->');
6 Comments
António Pereira
on 9 Jan 2019
Adam Danz
on 10 Jan 2019
For input '100110', the variable 't' (below) will be a vector with 601 elements.
t=0:0.01:length(bny); %length(bny) = 6
The for-loop (below) will have 6 iterations so the variable 'y' created within the for-loop will be a vector of 6 elements.
for j=1:length(bny) %length(bny) = 6
...
end
Then you're trying to plot y as a function of t (below) and to do this, 'y' and 't' need to have the same ammount of data. But your code produces 601 data points for 't' and only 6 data points for 'y' (for the inputs 100110)
plot(t,y,'k')
I haven't gone through your code to understand what you're trying to do but this explains the source of the error.
António Pereira
on 10 Jan 2019
Edited: António Pereira
on 10 Jan 2019
António Pereira
on 10 Jan 2019
Accepted Answer
More Answers (1)
KSSV
on 10 Jan 2019
YOu need to rethink on your code.
bny = input('Insira o codigo binario a codificar: ' ,'s');
idx = ismember(bny,'01');
assert(all(idx),'O codigo binario só pode conter 0s e 1s, mas contem o(s) número(s) %s',bny(~idx))
fprintf('O seu codigo é: %s\n',bny)
V = [-3,3];
n = V(bny-'/');
i=1;
a=0;
b=0.5;
% t=0:0.01:length(bny);
t = linspace(0,length(bny),length(bny)) ;
for j=1:length(bny)
if t(j)>=a && t(j)<=b
y(j)=V(i);
elseif t(j)>b && t(j)<=i
y(j)=0;
else
i= i+1;
a=a+1;
b=b+1;
end
end
plot(t,y,'k');axis([0 length(bny) -5 5]);title('Rz Polar');
xlabel('time-->');
ylabel('Amplitude-->');
1 Comment
António Pereira
on 10 Jan 2019
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
