How to assign output array to array using indexes
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ronald
am 12 Nov. 2017
Beantwortet: Star Strider
am 12 Nov. 2017
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
0 Kommentare
Akzeptierte Antwort
the cyclist
am 12 Nov. 2017
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.
0 Kommentare
Weitere Antworten (1)
Star Strider
am 12 Nov. 2017
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’.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!