Uniquetol which preserves the first occurance

2 ビュー (過去 30 日間)
ADSW121365
ADSW121365 2022 年 5 月 31 日
コメント済み: ADSW121365 2022 年 5 月 31 日
I'm using the uniquetol function to filter some data by rows. Here each column gives a weight value with respect to a specific basis function, and therefore each row gives the set of weights related to a specific function:
[~,index] = uniquetol(DATA,2.5e-1,'ByRows',true);
The DATA is sorted beforehand so the 'simplest' function is in row 1, and the most 'complex' function is in row N, with the practical aim being to exclude any 'complex' functions which are within tolerence to a simpler counterpart.
For this purpose, I'm looking for a way to ensure the index values output correspond to the first occurance in the DATA matrix, rather than choosing the 'highest' or 'lowest' occurance.
I searched the file-exchange, but there are a suprisingly large variety of unique-tolerence functions so I may have missed a valid existing solution.

採用された回答

Bruno Luong
Bruno Luong 2022 年 5 月 31 日
編集済み: Bruno Luong 2022 年 5 月 31 日
Use the third inputs followed by accumarray
% Dummy data
n = 100;
A = randi(10,1,n)+1e-12*rand(1,n)
A = 1×100
3.0000 9.0000 6.0000 10.0000 2.0000 7.0000 4.0000 9.0000 3.0000 9.0000 10.0000 3.0000 7.0000 6.0000 9.0000 3.0000 2.0000 6.0000 3.0000 5.0000 9.0000 8.0000 7.0000 3.0000 4.0000 4.0000 1.0000 4.0000 2.0000 4.0000
[~,~,J]=uniquetol(A);
firstidx = accumarray(J,(1:length(J))',[],@min)
firstidx = 10×1
27 5 1 7 20 3 6 22 2 4
% Check
A(firstidx)
ans = 1×10
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000
  1 件のコメント
ADSW121365
ADSW121365 2022 年 5 月 31 日
Beautiful solution, thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by