Replacing row number with value

4 ビュー (過去 30 日間)
Moe
Moe 2014 年 5 月 6 日
コメント済み: Moe 2014 年 5 月 6 日
Hi everyone,
Suppose I have a matrix a:
a = [2 2;1 1;4 1;3 1;4 2];
and matrix b:
b = [3;5;1;5;2;4;2;3;5;1];
Then I want matrix c to be:
c = [4 1;4 2;2 2;4 2;1 1;3 1;1 1;4 1;4 2;2 2]
b is included row number of matrix a and matrix c has replaced each row number in matrix b with value number from matrix a.
Can anyone please help me?

採用された回答

Jie Yang
Jie Yang 2014 年 5 月 6 日
編集済み: Jie Yang 2014 年 5 月 6 日
for i=1:length(b)
c(i,:)=a(b(i),:);
end
  1 件のコメント
Moe
Moe 2014 年 5 月 6 日
Thanks.

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

その他の回答 (1 件)

Cedric
Cedric 2014 年 5 月 6 日
A simple way is:
c = a(b,:) ;
  1 件のコメント
Moe
Moe 2014 年 5 月 6 日
Simple and perfect!

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by