Vectors must be the same length error
Mostrar comentarios más antiguos
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 comentarios
António Pereira
el 9 de En. de 2019
Adam Danz
el 10 de En. de 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
el 10 de En. de 2019
Editada: António Pereira
el 10 de En. de 2019
António Pereira
el 10 de En. de 2019
Respuesta aceptada
Más respuestas (1)
KSSV
el 10 de En. de 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 comentario
António Pereira
el 10 de En. de 2019
Categorías
Más información sobre Variables en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

