交叉指数按行(Intersection indices by rows)

编程入门 行业动态 更新时间:2024-10-19 16:33:47
交叉指数按行(Intersection indices by rows)

鉴于这两个矩阵:

m1 = [ 1 1; 2 2; 3 3; 4 4; 5 5 ]; m2 = [ 4 2; 1 1; 4 4; 7 5 ];

我正在寻找一个功能,例如:

indices = GetIntersectionIndecies (m1,m2);

其输出将是

indices = 1 0 0 1 0

如何在不使用循环的情况下找到这两个矩阵之间的行的交集索引?

Given these two matrices:

m1 = [ 1 1; 2 2; 3 3; 4 4; 5 5 ]; m2 = [ 4 2; 1 1; 4 4; 7 5 ];

I'm looking for a function, such as:

indices = GetIntersectionIndecies (m1,m2);

That the output of which will be

indices = 1 0 0 1 0

How can I find the intersection indices of rows between these two matrices without using a loop ?

最满意答案

一种可能的方案:

function [Index] = GetIntersectionIndicies(m1, m2) [~, I1] = intersect(m1, m2, 'rows'); Index = zeros(size(m1, 1), 1); Index(I1) = 1;

顺便说一句,我喜欢@Shai的创造性解决方案,如果你的矩阵很小,它比我的解决方案快得多。 但如果你的矩阵很大,那么我的解决方案将占主导地位。 这是因为如果我们设置T = size(m1, 1) m1,1 T = size(m1, 1) ,那么@Shai的答案中的tmp变量将是T * T,即如果T很大则是非常大的矩阵。 这是一些快速测试的代码:

%# Set parameters T = 1000; M = 10; %# Build test matrices m1 = randi(5, T, 2); m2 = randi(5, T, 2); %# My solution tic for m = 1:M [~, I1] = intersect(m1, m2, 'rows'); Index = zeros(size(m1, 1), 1); Index(I1) = 1; end toc %# @Shai solution tic for m = 1:M tmp = bsxfun( @eq, permute( m1, [ 1 3 2 ] ), permute( m2, [ 3 1 2 ] ) ); tmp = all( tmp, 3 ); % tmp(i,j) is true iff m1(i,:) == m2(j,:) imdices = any( tmp, 2 ); end toc

设置T = 10和M = 1000 ,我们得到:

Elapsed time is 0.404726 seconds. %# My solution Elapsed time is 0.017669 seconds. %# @Shai solution

但设置T = 1000和M = 100 ,我们得到:

Elapsed time is 0.068831 seconds. %# My solution Elapsed time is 0.508370 seconds. %# @Shai solution

One possible solution:

function [Index] = GetIntersectionIndicies(m1, m2) [~, I1] = intersect(m1, m2, 'rows'); Index = zeros(size(m1, 1), 1); Index(I1) = 1;

By the way, I love the inventive solution of @Shai, and it is much faster than my solution if your matrices are small. But if your matrices are large, then my solution will dominate. This is because if we set T = size(m1, 1), then the tmp variable in the answer of @Shai will be T*T, ie a very large matrix if T is large. Here's some code for a quick speed test:

%# Set parameters T = 1000; M = 10; %# Build test matrices m1 = randi(5, T, 2); m2 = randi(5, T, 2); %# My solution tic for m = 1:M [~, I1] = intersect(m1, m2, 'rows'); Index = zeros(size(m1, 1), 1); Index(I1) = 1; end toc %# @Shai solution tic for m = 1:M tmp = bsxfun( @eq, permute( m1, [ 1 3 2 ] ), permute( m2, [ 3 1 2 ] ) ); tmp = all( tmp, 3 ); % tmp(i,j) is true iff m1(i,:) == m2(j,:) imdices = any( tmp, 2 ); end toc

Set T = 10 and M = 1000, and we get:

Elapsed time is 0.404726 seconds. %# My solution Elapsed time is 0.017669 seconds. %# @Shai solution

But set T = 1000 and M = 100 and we get:

Elapsed time is 0.068831 seconds. %# My solution Elapsed time is 0.508370 seconds. %# @Shai solution

更多推荐

本文发布于:2023-08-07 02:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458477.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指数   Intersection   rows   indices

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!