I have two large matrices a and b. for instance
a=[1 2 3;4 5 6; 7 8 9; 10 11 12];
b=[1 3 2; 2 1 3; 3 2 1; 3 1 2];
how to get the matrix a in the form:
a = [1 3 2; 5 4 6; 9 8 7; 12 10 11]
This is actually the permutation of matrix a w.r.t. matrix b.
How to achieve this goal in matlab?

 채택된 답변

Stephen23
Stephen23 2018년 12월 30일
편집: Stephen23 2018년 12월 30일

0 개 추천

Use sub2ind, e.g.:
>> a = [1,2,3;4,5,6;7,8,9;10,11,12]
a =
1 2 3
4 5 6
7 8 9
10 11 12
>> b = [1,3,2;2,1,3;3,2,1;3,1,2]
b =
1 3 2
2 1 3
3 2 1
3 1 2
>> S = size(a);
>> [R,~] = ndgrid(1:S(1),1:S(2));
>> a = a(sub2ind(S,R,b))
a =
1 3 2
5 4 6
9 8 7
12 10 11

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 12월 30일

0 개 추천

a((b-1) * size(a,1) + (1:size(a,1)).')
This requires R2016b or later.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2017a

질문:

2018년 12월 30일

댓글:

2018년 12월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by