Ordering values in columns of an array according to the closest value in the previous column
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
the cyclist
2016-12-11
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 个评论
the cyclist
2016-12-11
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!