필터 지우기
필터 지우기

Assign values of array to indices of another array

조회 수: 13 (최근 30일)
Georg Bauer
Georg Bauer 2022년 8월 16일
댓글: Georg Bauer 2022년 8월 16일
Hello!
I'm not really sure how to word my question (hence the confusing title!). I do have to arrays:
A = [1,2;3,4];
B = [11;22;33;44];
Now I would like to arrange the elements of B in the order of array A. The output should then be like that:
B(A) = [11,22;33,44]
But now I wanna leave some places of the array 0. So at position A(1,1) where there is a zero shouldn't be a vallue of B asignd to (B(1,1) therefore should stay 0 as well).
A = [0,2;3;4];
B(A) = [0,22;33;44]
Thank you!
  댓글 수: 1
James Tursa
James Tursa 2022년 8월 16일
Need more details. Is the indexing in A always in that order, except for some of the spots might be 0? Or could the indexing be in a different order such as A = [2,0;4,3]?

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

채택된 답변

Chunru
Chunru 2022년 8월 16일
A = [1; 2; 3; 4];
B = [11; 22; 33; 44];
B(A)
ans = 4×1
11 22 33 44
A = [0; 2; 3; 4];
C = zeros(size(A));
C(A>0) = B(A(A>0))
C = 4×1
0 22 33 44

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by