Ordering values in columns of an array according to the closest value in the previous column
3 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
the cyclist
on 11 Dec 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 Comments
the cyclist
on 11 Dec 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.
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!