필터 지우기
필터 지우기

How to 'carry' matrix index with matrix operations

조회 수: 2 (최근 30일)
Xiaohan Du
Xiaohan Du 2016년 11월 15일
편집: Roger Stafford 2016년 11월 15일
Hi all,
I'd like to keep the matrix index while carrying out matrix operations, for example:
a = randi([1 20], 1, 5)';
indx = find(a);
c = [indx a];
b = sort(a);
I obtain:
a = [15 10 2 5 19]'; c = [1 2 3 4 5; 15 10 2 5 19]'; b = [2 5 10 15 19]';
After sort, how can I keep the old correspondence of index and vector in new matrix b? i.e. I want b to be something like this:
b = [3 4 2 1 5; 2 5 10 15 19]';
Thanks!

채택된 답변

Roger Stafford
Roger Stafford 2016년 11월 15일
편집: Roger Stafford 2016년 11월 15일
Use the second quantity returned by the 'sort' function:
[b,p] = sort(a);
The 'p' is an index for rearranging the first row from 1,2,3,4,5 to 3,4,2,1,5 with indx(p).
  댓글 수: 1
Xiaohan Du
Xiaohan Du 2016년 11월 15일
Thanks Roger! I should have checked the function help!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by