필터 지우기
필터 지우기

How to assign output array to array using indexes

조회 수: 1 (최근 30일)
Ronald
Ronald 2017년 11월 12일
답변: Star Strider 2017년 11월 12일
Hey guys,
I have an array called A which has a list of elements in a form of [18 4 5 8 1]. Then I sort A and reach in the following form of [1 4 5 8 18]. Based the sorted array of A, I put it in a function to generate a list of outputs [7 8 9 4 5]. Using the new function outputs, how can I assign the outputs to the original form of A? I know using the built-in functions of "find", but I don't know to use it in a right context. Cheers Ray

채택된 답변

the cyclist
the cyclist 2017년 11월 12일
I am not certain I understand your question. However, I think what you need is the second output argument of the sort command. If you sort A with
[sortedA,sortingIndex] = sort(A)
then the variable sortingIndex gives you the mapping from the unsorted A to sortedA.

추가 답변 (1개)

Star Strider
Star Strider 2017년 11월 12일
I believe I understand what you want to do.
Try this:
A = [18 4 5 8 1];
[As,Ix] = sort(A);
Fcn_Output = [7 8 9 4 5];
Result = Fcn_Output(Ix)
Result =
5 8 9 4 7
The sort function returns an optional second output that are the indices of the sorted argument vector ‘A’. The ‘Result’ vector uses this index vector to rearrange ‘Fcn_Output’ to match the sorted ‘A’.

카테고리

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