Find positions of elements in rows of one matrix in rows of another matrix (efficiency question)

1 回表示 (過去 30 日間)
Hello.
Im trying to process 2 big matrices in the following way:
a = perms(1:9);
b = perms(1:9);
c = zeros(size(a));
[~,m] = size(a);
tic;
for i = 1:m
c(bsxfun(@eq, a, b(:,i))) = i;
end
toc
Note that a and b are generated with perms just for example purposes.
Can this code be improved to run faster?
Thanks!

採用された回答

Matt J
Matt J 2021 年 6 月 27 日
編集済み: Matt J 2021 年 6 月 29 日
N=500;
a = randi(N,N);
b = randi(N,N);
c1 = zeros(size(a));
[~,m] = size(a);
tic;
for i = 1:m
c1(bsxfun(@eq, a, b(:,i))) = i;
end
toc
Elapsed time is 0.399945 seconds.
tic;
map=reshape(fliplr(b),[],1,m)==a;
[map,c]=max(map,[],3);
c=(m+1-c).*map;
toc
Elapsed time is 0.178260 seconds.
isequal(c1,c)
ans = logical
1
  3 件のコメント
Somebody
Somebody 2021 年 7 月 1 日
Thanks alot!
It realy works faster.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by