Azzera filtri
Azzera filtri

How to assign output array to array using indexes

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

the cyclist
the cyclist il 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.

Più risposte (1)

Star Strider
Star Strider il 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’.

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by