MATLAB有效地找到包含在一个大的矩阵二三元素的行 [英] MATLAB Efficiently find the row that contains two of three elements in a large matrix

查看:253
本文介绍了MATLAB有效地找到包含在一个大的矩阵二三元素的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的矩阵,姑且称之为A,它具有尺寸MX3,例如M = 4000排3列。在矩阵的每行包含三个数字,例如。 [241 112 478。在这些三个数字中,我们可以构建三双,例如。 [241 112],[112 478],[241 478]。其他3999行:


  • 对于每三对,M的中的一行(只有一个)将包含相同的对。然而,这些数字的顺序可以被加扰。例如,只有一行将读取:333 478 112。没有其他行都将同时拥有478和112的我感兴趣的是找到该行的索引,每个三对的。输出应该然后是另一个矩阵,称之为B,用相同的尺寸4000x3,其中每一行有在原始矩阵A共享一对数字的行的索引。

  • 没有其他行都将包含相同的三个数字。

  • 其他行可能没有包含的数字或一个数字。

下面是实现这一功能,但速度很慢 - 我想知道是否有更有效的方式。在此先感谢!

  M =尺寸(A,1); %没有元素B =零(M,3);对于j = 1:M
   L = 1;
   K = 1;
   而L 4;%存在不能超过3
      如果k〜= j的
         S = SUM(ismember(A(J,:),A(K,:)));
         如果s == 2
            B(J,L)= K;
            L = L + 1;
         结束
   结束
   K = K + 1;
结束


不需要

解决方案

有关循环,只要使用 ismember 通过以下方式:

  row_id1 =找到(SUM(ismember(男,[241 112]); 2)→1);
 row_id2 =查找(总和(ismember(M,[478 112]),2)→1);
 row_id3 =查找(总和(ismember(M,[478 241]),2)→1);

每个 ROW_ID 会给你对在该行的行索引,无论出现在它的顺序。

在这里所作的唯一前提是,你找意志对一个号码的不可以连续出现两次(即 [112 333 112] )。如果这个假设是错误的,这可以使用来解决唯一

I have a large matrix, let's call it A, which has dimension Mx3, e.g. M=4000 rows x 3 columns. Each row in the matrix contains three numbers, eg. [241 112 478]. Out of these three numbers, we can construct three pairs, eg. [241 112], [112 478], [241 478]. Of the other 3999 rows:

  • For each of the three pairs, exactly one row of M (only one) will contain the same pair. However, the order of the numbers could be scrambled. For example, exactly one row will read: [333 478 112]. No other row will have both 478 and 112. I am interested in finding the index of that row, for each of the three pairs. The output should then be another matrix, call it B, with same dimensions 4000x3, where each row has the indices of the rows in the original matrix A that share a pair of numbers.
  • No other row will contain the same three numbers.
  • Other rows may contain none of the numbers or one of the numbers.

Here's a function that accomplishes this, but is very slow - I'd like to know if there is a more efficient way. Thanks in advance!

M=size(A,1); % no elements

B=zeros(M,3);

for j=1:M
   l=1;
   k=1;
   while l<4 % there cant be more than 3
      if k~=j
         s=sum(ismember(A(j,:),A(k,:)));
         if s==2
            B(j,l)=k;
            l=l+1;
         end
   end
   k=k+1;
end

解决方案

For loops are not needed, just use ismember the following way:

 row_id1=find(sum(ismember(M,[241 112]),2)>1);
 row_id2=find(sum(ismember(M,[478 112]),2)>1);
 row_id3=find(sum(ismember(M,[478 241]),2)>1);

each row_id will give you the row index of the pair in that line regardless of the order at which it appears.

The only assumption made here is that one of the pair numbers you look for will not appear twice in a row, (i.e. [112 333 112]). If that assumption is wrong, this can be addressed using unique.

这篇关于MATLAB有效地找到包含在一个大的矩阵二三元素的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆