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!