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개)

Cedric
Cedric 2014년 5월 6일
A simple way is:
c = a(b,:) ;
  댓글 수: 1
Moe
Moe 2014년 5월 6일
Simple and perfect!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by