2013-11-01 25 views
1

此列表的真實生活狀況良好的排序方式,並保持原有的列表索引在Matlab

list = [0.2 0.1 0.3 0.4 0.7 0.5 0.6 0.9 1.0]; 

最小的例子,我對它進行排序

sorted_list = sort(list, 'descend'); 

我需要得到在列表中具有最高值的指數的10%。

我嘗試

% Take the amount of indexes to 10% 
limit = size(sorted_list); 
size = limit(1); 
limit = ceil(0.1*size); 

% find the index numbers from the original list which corresponds to the highest indexes 
for j = 1:limit 
    value = sorted_list(j); 
    for k = 1:size 
     if value == list(k) 
      refine_set(j) = k; 
      % here much resources used, should be able stop if matching 
      % early, so should be able to stop the for-loop somehow 
      % I do not want to use while-loop, since in some cases, this would cause 
      % infinite loop 
     end; 
    end; 
end; 

我開始認爲必須有一個更好的方式來做到這一點。 功能最大似乎沒有一個參數,允許我採取那些代表10%的最大值的指標。

什麼是一個很好的方式來獲取原始列表的索引,這些索引代表列表中10%的最大值?

什麼是這個任務的良好數據結構?

回答

2

Matlab具有分類功能與兩個輸出值:

[B,IX] = sort(A,...) 

IX要接收排序後的數組所需索引的排列。

作爲導致需要下面的算法:

[sorted_list, IX] = sort(list, 'descend'); 
limit = length(sorted_list); 
limit = ceil(0.1 * limit); 
refine_set = IX(1:limit); 

注意:這是更好地利用功能lengthnumel代替size以限定在陣列的情況下,元素的數量,因爲函數size具有兩個輸出端(行數和列數),並且可能會錯誤地使用行數(等於1)而不是列數。