How to get origin vector form indexd matrix

I have a 1xK row vector A generated by randi function, and K is even number
K=6;
A = randi([50 500],1,K);
A will rearrage by a K/2x1 index matrix which the index matrix is obtained from some algorithm, for example
idx = [1 3;4 5;2 6];
B = A(idx);
I want to get a vector C same as A from B, but I can't do
C = A
in my case. How should I do?
Very appreciate for any suggestion.

 채택된 답변

Jan
Jan 2021년 5월 18일
편집: Jan 2021년 5월 18일
K = 6;
A = randi([50 500], 1, K)
A = 1×6
71 409 445 202 193 95
idx = [1 3;4 5;2 6];
B = A(idx)
B = 3×2
71 445 202 193 409 95
C(idx) = B
C = 1×6
71 409 445 202 193 95
Of course, because if you omit the indirection over B you have:
C(idx) = A(idx)
which is an exact copy.

추가 답변 (1개)

Scott MacKenzie
Scott MacKenzie 2021년 5월 18일

0 개 추천

idx2 = [1 3 4 2 5 6];
C = B(idx2)

댓글 수: 2

WJ
WJ 2021년 5월 18일
Thanks for answer, but the idx will be dynamic not fix.
Scott MacKenzie
Scott MacKenzie 2021년 5월 18일
편집: Scott MacKenzie 2021년 5월 18일
Ok, got it. See Jan's answer. He's nailed it.

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

제품

릴리스

R2019b

태그

질문:

WJ
2021년 5월 18일

편집:

2021년 5월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by