用matlab中某些列值的条件查找矩阵的行 [英] Find the rows of a matrix with conditions concerning the values of certain columns in matlab

查看:1113
本文介绍了用matlab中某些列值的条件查找矩阵的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我想查找Matlab矩阵中的所有行,在某些列中,行中的值与前一行中的值相等,或者一般而言,在矩阵中的某行中相等。例如,我有一个矩阵

  1 2 3 4 
1 2 8 10
4 5 7 9
2 3 6 4
1 2 4 7

我想查找以下行:

  1 2 3 4 
1 2 3 10
1 2 4 7

我该如何做这样的事情,以及如何对列1和2,并且在前面的行中有相同的值,存在于矩阵中?

解决方案

这里有一个开始,看看我们是正确的方向:

 >> M = [1 2 3 4; 
1 2 8 10;
4 5 7 9;
2 3 6 4;
1 2 4 7];

>> N = M; %//将M复制到一个新的矩阵中,这样我们可以修改它
>> idx = ismember(N(:1:2),N(1,1:2),'rows')

idx =

1
1
0
0
1

>> N(idx,:)
ans =

1 2 3 4
1 2 8 10
1 2 4 7


然后,您可以从原始矩阵中移除这些行并重复。

 >> N = N(〜idx,:) 
N =

4 5 7 9
2 3 6 4


As the title says, I want to find all rows in a Matlab matrix that in certain columns the values in the row are equal with the values in the previous row, or in general, equal in some row in the matrix. For example I have a matrix

1 2 3 4
1 2 8 10
4 5 7 9
2 3 6 4
1 2 4 7

and I want to find the following rows:

1 2 3 4 
1 2 3 10
1 2 4 7

How do I do something like that and how do I do it generally for all the possible pairs in columns 1 and 2, and have equal values in previous rows, that exist in the matrix?

解决方案

Here's a start to see if we're headed in the right direction:

>> M = [1 2 3 4;
     1 2 8 10;
     4 5 7 9;
     2 3 6 4;
     1 2 4 7];

>> N = M;  %// copy M into a new matrix so we can modify it
>> idx = ismember(N(:,1:2), N(1,1:2), 'rows')

idx =

   1
   1
   0
   0
   1

>> N(idx, :)
ans =

    1    2    3    4
    1    2    8   10
    1    2    4    7

Then you can remove those rows from the original matrix and repeat.

>> N = N(~idx,:)
N =

   4   5   7   9
   2   3   6   4

这篇关于用matlab中某些列值的条件查找矩阵的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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