how to form a new matrix (i.e. B) from two different matrices (i.e. a1, a2) knowing the indices of its origional matrix (indices of a)?

3 ビュー (過去 30 日間)
% a = [1 2 3 4 5 6 7; 2 4 6 8 10 12 14]; % dimension: 2x7
a1 = [2 6 12]; % index in a = [1 3 6] % dimension: 1x3
a2 = [4 8 10 14]; % index in a = [2 4 5 7] % dimension: 1x4
% Now how to form a new matrix "B" such that B = [2 4 6 8 10 12 14]; using a1 and a2. % dimension: 1x7
% Here we know the position of each index in a1 and a2 to be put in new matrix B.
So, how to form a new matrix (i.e. B) from two different matrices (i.e. a1, a2) knowing the indices of its origional matrix (indices of a)?

採用された回答

Stephen23
Stephen23 2021 年 2 月 25 日
編集済み: Stephen23 2021 年 2 月 25 日
You can use those indices on the left-hand side. For example:
a1 = [2,6,12];
x1 = [1,3,6];
a2 = [4,8,10,14];
x2 = [2,4,5,7];
% Try
b = [a1,a2];
b([x1,x2])= b
b = 1×7
2 4 6 8 10 12 14
% Or
b = nan(1,7);
b(x1) = a1;
b(x2) = a2
b = 1×7
2 4 6 8 10 12 14

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by