Effacer les filtres
Effacer les filtres

How to assign output array to array using indexes

1 vue (au cours des 30 derniers jours)
Ronald
Ronald le 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

Réponse acceptée

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

Plus de réponses (1)

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

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by