Borrar filtros
Borrar filtros

Ordering values in columns of an array according to the closest value in the previous column

2 visualizaciones (últimos 30 días)
I have an array with complex numbers. Each column of the array corresponds to a new parameter. I would like to order the values of each column so that they are on the same row as the values on the previous column that are closest to them. At the moment, the code produces the two values between adjacent columns which are the closest rather than pairing up the values between adjacent columns that are the closest. Ultimately, I would like a new array with the values in their new order. Many thanks.
for j = 1:1:n-1;
difference = abs(eigenvaluearray(:,j)- eigenvaluearray(:,j+1));
[min,index] = min(difference)
orderedeigenvalues(index) = eigenvaluearray(j)
end
The type of ordering I'm looking for is (example with real numbers):
The values need to be ordered so that values in each column are paired up with the value in the previous column which is closest to it. E.g:
1 2
3 5
4 0
After reordering, this would give:
1 0
3 2
4 5
Because the differences between the values in the rows are the minimum.

Respuestas (1)

the cyclist
the cyclist el 11 de Dic. de 2016
I don't fully understand what you are trying to do, but I wonder if the sort command would be more useful than the min command.
  2 comentarios
belle
belle el 11 de Dic. de 2016
The sort command won't compare the columns and will only sort the values in each column independently of other columns (I think).
The values need to be ordered so that values in each column are paired up with the value in the previous column which is closest to it. E.g:
1 2
3 5
4 0
After reordering, this would give:
1 0
3 2
4 5
Because the differences between the values in the rows are the minimum.
the cyclist
the cyclist el 11 de Dic. de 2016
Right. So I was thinking something along the lines of
difference = abs(eigenvaluearray(:,j)- eigenvaluearray(:,j+1));
[min,index] = sort(difference)
using sort in place of min in your code.

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by