阵列中最小的N个元素及其位置

Nea*_*mah 0 matlab

我有一个名为Acc_Std1行和222列的数组.

我需要100在每个数组中具有最小值但是具有其原始位置.

我写了这段代码但实际上不起作用:

for Col = 1:222 

    [Std_Cont, Std_Loc]  = min(Acc_Std(:));
    Sort_Std_Cont(Col,1) = Std_Cont;
    Sort_Std_Loc(Col,1)  = Std_Loc;

    Acc_Std(Std_Loc) = []; % Here is the problem in my code
end 
Run Code Online (Sandbox Code Playgroud)

Rod*_*uis 5

使用以下两个输出sort:

% Example data
Acc_Std = randi(10, 1,10);

% Extract the smallest N elements
N = 3;

% Sort, while saving the original indices
[B, indices] = sort(Acc_Std);

% Now extract the N smallest elements
smallest_N = B(1:N);

% Check that they are indeed located at the
% indices returned by sort()
isequal(smallest_N, Acc_Std(indices(1:N)))
Run Code Online (Sandbox Code Playgroud)

执行这个小脚本的结果:

ans =
     1
Run Code Online (Sandbox Code Playgroud)