简体   繁体   English

MATLAB有效地在大矩阵中找到包含三个元素中的两个元素的行

[英]MATLAB Efficiently find the row that contains two of three elements in a large matrix

I have a large matrix, let's call it A, which has dimension Mx3, eg M=4000 rows x 3 columns. 我有一个大矩阵,我们称它为A,其尺寸为Mx3,例如M = 4000行×3列。 Each row in the matrix contains three numbers, eg. 矩阵中的每一行包含三个数字,例如。 [241 112 478]. [241 112 478]。 Out of these three numbers, we can construct three pairs, eg. 在这三个数字中,我们可以构造三对,例如。 [241 112], [112 478], [241 478]. [241 112],[112 478],[241 478]。 Of the other 3999 rows: 在其他3999行中:

  • For each of the three pairs, exactly one row of M (only one) will contain the same pair. 对于三对中的每一对,恰好一行M(仅一行)将包含相同的对。 However, the order of the numbers could be scrambled. 但是,数字的顺序可能会被扰乱。 For example, exactly one row will read: [333 478 112]. 例如,恰好一行将为:[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. 没有其他行同时具有478和112. 我有兴趣为三对中的每一对找到该行的索引。 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. 然后输出应该是另一个矩阵,称为B,具有相同的维度4000x3,其中每行具有原始矩阵A中共享一对数字的行的索引。
  • 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: 对于不需要的循环,只是用ismember方式如下:

 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. 每个row_id将为您提供该行中该对的行索引,而不管它出现的顺序如何。

The only assumption made here is that one of the pair numbers you look for will not appear twice in a row, (ie [112 333 112] ). 这里唯一的假设是你寻找的一对数字不会连续出现两次(即[112 333 112] )。 If that assumption is wrong, this can be addressed using unique . 如果这个假设是错误的,可以使用unique来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何有效地找到2D矩阵中多个元素的行索引? - How do I efficiently find the row indices of multiple elements in a 2D matrix? 在两个值之间随机分配矩阵元素,同时保持行和列之和固定(MATLAB) - Randomize matrix elements between two values while keeping row and column sums fixed (MATLAB) 如何有效地过滤每行矩阵的最大元素 - How to efficiently filter maximum elements of a matrix per row 将矩阵的每一行中的三个元素相加-无循环 - Sum three elements in every row of the matrix - without loops 如何将两列单元格数组转换为带点矩阵(单元格数组每一行中的每对元素)MATLAB - how to convert two-columned cell array into matrix with points (each pair of elements from each row of cell array) MATLAB Matlab,找到两个单元格数组的公共元素 - Matlab, find common elements of two cell arrays 在Matlab中对3维矩阵的每一行中的元素进行排序 - Sort elements in each row of a 3-d matrix in Matlab 在 Matlab 中构造一个矩阵,跟踪每行中的相等元素 - Construct a matrix in Matlab that keeps track of equal elements in each row 如何在Matlab矩阵的每一行中删除多余的重复元素? - How to remove extra duplicated elements in each row of a matrix in matlab? 查找矩阵中特定元素的按行索引并填充另一个矩阵 - find row wise index of specific elements in a matrix and populate another matrix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM