Borrar filtros
Borrar filtros

How to assign output array to array using indexes

1 visualización (últimos 30 días)
Ronald
Ronald el 12 de Nov. de 2017
Respondida: Star Strider el 12 de Nov. de 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

Respuesta aceptada

the cyclist
the cyclist el 12 de Nov. de 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.

Más respuestas (1)

Star Strider
Star Strider el 12 de Nov. de 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’.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by