Fast method count unique variables
古いコメントを表示
Ok so given an array of (n,m) dimensions populated with positive integers from 1:m and each row of the array will contain only one of each integer: so no repeats.
For example:
2,1,3,4
3,1,2,4
1,2,3,4
2,1,4,3
I'd like a fast method of creating arrays containing all the positions that the numbers fall into, for example:
One.positions = [1,2]
Two.positions = [1,2,3]
Three.postions = [1,3,4]
Four.positions = [3,4]
I'm doing this for arrays sometimes of sizes = [1e+6,30]; currently I'm using a loop moving column wise with accummarray and if the number exists in that Column it is iteratively added to an array.
Original Code:
% m = array containing sequences of integers
total = size(m,2)
A = (1:total);
intel_pos = cell(total,1);
for i = 1:total
for j = 1:total
if sum(A(accumarray(m(:,j),1) > 0) == i) == 1
intel_pos{i} = [intel_pos{i};j];
end
end
end
So obvious areas of improvement can be done.
採用された回答
その他の回答 (2 件)
Guillaume
2017 年 5 月 3 日
No idea if it's faster than your method, this does not need a loop:
m = [2 1 3 4;1 2 3 4;1 2 4 3;2 1 4 3]; %demo data
colindices = repmat(1:size(m, 2), size(m, 1), 1);
out = accumarray(m(:), colindices(:), [], @(x) {unique(x)})
2 件のコメント
Sean de Wolski
2017 年 5 月 3 日
Loops are not typically slower. This was true 15 years ago but with advances in the MATLAB execution engine and jit accelerator they are now at parity with vectorized operations much of the time.
Matthew Hickson
2017 年 5 月 3 日
編集済み: Matthew Hickson
2017 年 5 月 3 日
Sean de Wolski
2017 年 5 月 3 日
編集済み: Sean de Wolski
2017 年 5 月 4 日
I'd suspect a simple loop over columns with ismember would be very fast.
EDIT from Comment Clarification
tic
ncol = size(m,2);
nrow = size(m,1);
present = cell(ncol,1);
for ii = 1:ncol
present{ii} = unique(ceil(find(m==ii)./nrow));
end
toc
OLD
[~,m] = sort(rand(1e6,30),2);
tic
ncol = size(m,2)
present = cell(ncol,1);
for ii = 1:ncol
present{ii} = find(ismember(1:ncol,m(:,ii)));
end
toc
This is taking 0.9s on my laptop.
6 件のコメント
Matthew Hickson
2017 年 5 月 3 日
Sean de Wolski
2017 年 5 月 3 日
Can you be more specific? I don't understand the difference between "allowed" and "at a given position". Maybe a counter example with the same original data set?
Matthew Hickson
2017 年 5 月 3 日
編集済み: Matthew Hickson
2017 年 5 月 3 日
Sean de Wolski
2017 年 5 月 4 日
See EDIT
Matthew Hickson
2017 年 5 月 4 日
編集済み: Matthew Hickson
2017 年 5 月 4 日
David Goodmanson
2017 年 5 月 5 日
Hi Matthew, I have done a revised answer, which is about three times faster than the code listed above.
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!