Bhilding new matrix with selected index number

1 回表示 (過去 30 日間)
Moe
Moe 2014 年 10 月 29 日
回答済み: Orion 2014 年 10 月 29 日
Suppose I have a matrix a:
a = [9;1;3;2;5;6;8;1;2;5;8;2];
And I have another matrix b which is like some selected index number of matrix a:
b = [4;6;1]; % for example 4 means forth row in matrix a which is 2 here
So, I need to have a new matrix c in that way present those rows in matrix a which are indicated in matrix b and others array be equal to 0:
c = [9,0,0,2,0,6,0,0,0,0,0,0]; % for example first array is 9 because the first row is (1) is in the matrix b and the value is equal 9 because first row in matrix a is 9.

採用された回答

Orion
Orion 2014 年 10 月 29 日
just use b as an index vector :
a = [9;1;3;2;5;6;8;1;2;5;8;2];
b = [4;6;1];
c=zeros(1,length(a));
c(b) = a(b);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by