在Matlab中对向量进行排序 [英] sort vector in matlab
问题描述
我有以下代码来计算峰及其指数并显示它们,但是我想对峰值进行排序并显示出来,所以我的代码正在跟踪
i have following code which calculates peaks and their indexes and also display them,but i want to sort peaks value and display so,so my code is following
function [peaks,peak_indices] = find_peaks(row_vector)
A = [0 row_vector 0];
j = 1;
for i=1:length(A)-2
temp=A(i:i+2);
if(max(temp)==temp(2))
peaks(j) = row_vector(i);
peak_indices(j) = i;
j = j+1;
end
end
end
通过以下方式实现后,会向我显示输出
which after implementing it by following way,it shows me output
A = [2 1 3 5 4 7 6 8 9];
>> [peaks, peak_indices] = find_peaks(A)
peaks =
2 5 7 9
peak_indices =
1 4 6 9
但是我不想直接显示峰,而是想按降序显示峰,或者像这样 9 7 5 2,我知道在matlab中存在函数排序,就像下面的方式
but instead of display peaks directly,i want to display peaks in decreasing order ,or like this 9 7 5 2,i know that there exist function sort in matlab,like following way
b=[2 1 3 4 6 5];
>> sort(b)
ans =
1 2 3 4 5 6
但是有两个问题,首先是按升序排序,还有如何在我的第一个函数中使用sort函数以降序形式返回峰?
but there is two issues,first it sort in increasing order,also how to use sort function in my first function to return peaks in decreasing sorted form?
推荐答案
您可以这样做:
peaks = sort(peaks, 'descend')
要分别对peak_indices
重新排序,还可以从sort
获取排序的索引:
To re-order peak_indices
respectively, obtain the sorted indices from sort
as well:
[peaks, idx] = sort(peaks, 'descend'); %// Sort peaks
peak_indices = peak_indices(idx); %// Reorder peak indices accordingly
这篇关于在Matlab中对向量进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!