making two different vectors the same length

111 ビュー (過去 30 日間)
soloby
soloby 2015 年 6 月 13 日
コメント済み: Walter Roberson 2015 年 6 月 13 日
x1_lower = <1x100> vector
x2_lower = <1x110> vector
Im trying to plot (x1_lower+ x2_lower, y)
what's the best way of making them an equal length? or is there a way to get around this? thanks
  1 件のコメント
soloby
soloby 2015 年 6 月 13 日
Ok, let me ask a simpler question.
how do i add zeroes to the beginning or these end of the vectors to the same length as something like x = -10:.1:10

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

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 6 月 13 日
minlen = min(length(x1_lower), length(x2_lower));
plot(x1_lower(1:minlen) + x2_lower(1:minlen), y)
However I would in general expect that to fail as y would probably not be the same length.
The above code assumes that the two vectors are on the same origin and spacing, so the extra elements for x2_lower represent elements beyond the end of x1_lower and each element x1_lower(K) corresponds to x2_lower(K). If that is not the case then you need information about the origin and increments in order to align the two.
  1 件のコメント
Walter Roberson
Walter Roberson 2015 年 6 月 13 日
To pad with zeros at the end:
maxlen = max(length(x1_lower), length(x2_lower));
x1_lower(end+1:maxlen) = 0;
x2_lower(end+1:maxlen) = 0;
To pad with zeros at the beginning:
maxlen = max(length(x1_lower), length(x2_lower));
x1_lower = [zeros(1,maxlen - length(x1_lower)), x1_lower];
x2_lower = [zeros(1,maxlen - length(x2_lower)), x2_lower];
This code should work no matter which of the two is shorter.

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

カテゴリ

Help Center および File ExchangePropagation and Channel Models についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by