How to arrange the values of the matrix according to index number?

조회 수: 1 (최근 30일)
AS
AS 2020년 9월 28일
답변: madhan ravi 2020년 9월 30일
I have index number of matrix A =[1 3 5 6], B= [ 2 4]. The values corresponding to index number matrix A is A1=[3 4 -7 5; 2 5 10 -9; -11 56 9 -4; 6 -7 4 20] and the values corresponding to index number matrix B is B1=[13 4 -70 51; -1 95 12 -9].
I want to generate a matrix C with index number of A and B after sorting like C=[ 1 2 3 4 5 6] and the values of the matrix C1 will arrange according to the index numb of C.
How will be arranging the values of C1 from A1 and B1 according to the index number C? Please help on this
  댓글 수: 7
AS
AS 2020년 9월 28일
편집: madhan ravi 2020년 9월 28일
C_new is showing index number only and its va;ue will be
C_value= 4 by 6
3 13 4 -1 -7 5
2 4 5 95 10 -9
-11 -70 56 12 9 -4
6 51 -7 -9 4 20
Final output will 4 by 6 matrix.
I have tried with taking C_value=zeros(4, 6) and then try to store the values from A1 and B1 with their proper index number. But unable to do.

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

채택된 답변

madhan ravi
madhan ravi 2020년 9월 28일
편집: madhan ravi 2020년 9월 28일
C_new = zeros(numel(A), numel([A, B]));
C_new(:, A) = A1;
C_new(:, B) = B1.'

추가 답변 (1개)

madhan ravi
madhan ravi 2020년 9월 30일
A faster version of the other answer:
C_new = zeros(numel(A), numel([A, B]));
[IX, ix] = sort([A, B]);
C_new(:, IX) = [A1, B1.'];
C_new = C_new(:, ix)

카테고리

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