How to speed up the "unique" function when sorting out unique columns
4 ビュー (過去 30 日間)
古いコメントを表示
As you can see by running the example code below, the time it takes to compute the number of unique columns quickly deteriorates with matrix size.
Others have proposed ways to write inline code in order to speed this up, but those solutions do not seem to cover column-wise (alt. row-wise) uniqueness.
Please help.
clear all, close all
n = 20; % #objects
% compute all possible combinations of 0 and 1 (#combinations = 2^n)
% 0 - object is turned off
% 1 - object is active
% furthermore, as an example the states 01 and 10 are assumed to be equivalent since order is not an issue
combs = [0 1];
for i = 2:n
combs = combvec(combs,[0 1]);
combsSort = sort(combs, 1); % sort columnwise
tic
uniqueCombs = unique(combsSort.','rows').'; % find unique columns by transposing twice
toc
size(uniqueCombs)
end
1 件のコメント
FirefoxMetzger
2016 年 8 月 15 日
This entire sorting can be solved by:
result = ones(N,N);
result = fliplr(tril(result));
To suggest a better algorithm (if still needed) please be more specific about what you want. The unique() command is the most efficient way to solve a general problem. However, there is often a more efficient implementation depending on the need.
回答 (0 件)
参考
カテゴリ
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!