Why do I get“Error using plot Vectors must be the same length."

Please tell me how to correct it.
Ts = 0.001;
fs = 1/Ts;
To = 0.5;
t = 0:Ts:To-Ts;
N = length(t);
d = [0 1 0 1;0 0 1 1;1 0 1 0;1 1 0 0];
Ibranch = d(1:2:end);
Qbranch = d(2:2:end);
Im = [];
Qm = [];
for k1 = 1:length(Ibranch)
Im = [Im Ibranch(k1)*ones(1, N)];
Qm = [Qm Qbranch(k1)*ones(1, N)];
end
figure(1);
subplot(2,1,1);
plot(t, Im);
Error using plot
Vectors must be the same length.
title('In-Phase Pulse Waveform');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2,1,2);
plot(t, Qm);
title('Quadrature Pulse Waveform');
xlabel('Time (s)');
ylabel('Amplitude');

1 件のコメント

Walter Roberson
Walter Roberson 2023 年 11 月 8 日
Im = [];
Qm = [];
for k1 = 1:length(Ibranch)
Im = [Im Ibranch(k1)*ones(1, N)];
Qm = [Qm Qbranch(k1)*ones(1, N)];
end
What are you trying to do? Have you considered repelem ? Not that it is at all clear to me why you are replicating the values.

サインインしてコメントする。

回答 (1 件)

Cris LaPierre
Cris LaPierre 2023 年 11 月 8 日
編集済み: Cris LaPierre 2023 年 11 月 8 日
Make sure t and Im have the same number of elements.
Ts = 0.001;
To = 0.5;
t = 0:Ts:To-Ts;
N = length(t);
d = [0 1 0 1;0 0 1 1;1 0 1 0;1 1 0 0];
Ibranch = d(1:2:end);
Im = [];
for k1 = 1:length(Ibranch)
Im = [Im Ibranch(k1)*ones(1, N)];
end
size(t)
ans = 1×2
1 500
size(Im)
ans = 1×2
1 4000

2 件のコメント

Chris
Chris 2023 年 11 月 8 日
Sorry, but what method should I use to improve it?
I'm not sure what you are trying to do. Best option may be to remove the for loop. Once you understand why this works, start adding to the code until you get a working final solution.
Ts = 0.001;
To = 0.5;
t = 0:Ts:To-Ts;
N = length(t);
d = [0 1 0 1;0 0 1 1;1 0 1 0;1 1 0 0];
Ibranch = d(1:2:end);
k1 = 1;
Im = Ibranch(k1)*ones(1, N);
size(t)
ans = 1×2
1 500
size(Im)
ans = 1×2
1 500
plot(t, Im);
title('In-Phase Pulse Waveform');
xlabel('Time (s)');
ylabel('Amplitude');

サインインしてコメントする。

カテゴリ

製品

リリース

R2023b

タグ

質問済み:

2023 年 11 月 8 日

コメント済み:

2023 年 11 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by