フィルターのクリア

How to add data of 2 vectors with different size

12 ビュー (過去 30 日間)
Jamais avenir
Jamais avenir 2015 年 4 月 18 日
コメント済み: Star Strider 2015 年 4 月 18 日
Hi
I have 2 vectors and I want to merge them directly. A=[1;2;3;4;5] and B=zeros(17,1) where, elements of A are going to these indices [4 5 7 11 13];
A_new=[0;0;0;1;2;0;3;0;0;0;4;0;5;0;0;0;0]

採用された回答

Star Strider
Star Strider 2015 年 4 月 18 日
One way:
B = zeros(17,1);
A = [1;2;3;4;5];
I = [4 5 7 11 13];
A_new = B;
A_new(I) = A;
  2 件のコメント
Jamais avenir
Jamais avenir 2015 年 4 月 18 日
編集済み: Jamais avenir 2015 年 4 月 18 日
Thanks, But I want to add them directly, for example like solving this case: If I had :
aa = [1 2 3 4]; bb = [100 100]; and wanted to get this result:
cc = [101 102 3 4]
The answer could be :
cc = aa + [bb zeros(1,2)];
Is there any similar answer ?
Star Strider
Star Strider 2015 年 4 月 18 日
That isn’t the same as in your original Question.
You have to address them by the appropriate subscripts, and it works:
aa = [1 2 3 4];
bb = [100 100];
cc = aa;
cc(1:2) = cc(1:2) + bb;
There may be other ways, but that’s the most efficient.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by