利用Matlab进行交集、并集等运算

Matlab是强大的数值分析与计算的软件,本例分享使用Matlab进行两个数集的交集、并集等运算。

intersect函数取交集

  • 01

    帮助文档: intersect Set intersection. C = intersect(A,B) for vectors A and B, returns the values common to the two vectors with no repetitions. C will be sorted. C = intersect(A,B,'rows') for matrices A and B with the same number of columns, returns the rows common to the two matrices. The rows of the matrix C will be in sorted order. [C,IA,IB] = intersect(A,B) also returns index vectors IA and IB such that C = A(IA) and C = B(IB). If there are repeated common values in A or B then the index of the last occurrence of each repeated value is returned. [C,IA,IB] = intersect(A,B,'rows') also returns index vectors IA and IB such that C = A(IA,:) and C = B(IB,:). [C,IA,IB] = intersect(A,B,'stable') for arrays A and B, returns the values of C in the same order that they appear in A. [C,IA,IB] = intersect(A,B,'sorted') returns the values of C in sorted order. If A and B are row vectors, then C will be a row vector as well, otherwise C will be a column vector. IA and IB are column vectors. If there are repeated common values in A or B then the index of the first occurrence of each repeated value is returned. [C,IA,IB] = intersect(A,B,'rows','stable') returns the rows of C in the same order that they appear in A. [C,IA,IB] = intersect(A,B,'rows','sorted') returns the rows of C in sorted order. In a future release, the behavior of the following syntaxes will change including: - occurrence of indices in IA and IB will switch from last to first - orientation of vector C - IA and IB will always be column index vectors - tighter restrictions on combinations of classes In order to see what impact those changes will have on your code, use: [C,IA,IB] = intersect(A,B,'R2012a') [C,IA,IB] = intersect(A,B,'rows','R2012a') If the changes in behavior adversely affect your code, you may preserve the current behavior with: [C,IA,IB] = intersect(A,B,'legacy') [C,IA,IB] = intersect(A,B,'rows','legacy') Examples: a = [9 9 9 9 9 9 8 8 8 8 7 7 7 6 6 6 5 5 4 2 1] b = [1 1 1 3 3 3 3 3 4 4 4 4 4 10 10 10] [c1,ia1,ib1] = intersect(a,b) % returns c1 = [1 4], ia1 = [21 19], ib1 = [3 13] [c2,ia2,ib2] = intersect(a,b,'stable') % returns c2 = [4 1], ia2 = [19 21]', ib2 = [9 1]' c = intersect([1 NaN 2 3],[3 4 NaN 1]) % NaNs compare as not equal, so this returns c = [1 3] Class support for inputs A and B, where A and B must be of the same class unless stated otherwise: - logical, char, all numeric classes (may combine with double arrays) - cell arrays of strings (may combine with char arrays) -- 'rows' option is not supported for cell arrays - objects with methods SORT (SORTROWS for the 'rows' option), EQ and NE -- including heterogeneous arrays derived from the same root class

  • 02

    简单示例:集合A: A = 1     2     3     4     5     6     7     8     9    10 集合B: > B=5:2:15 B = 5     7     9    11    13    15

  • 03

    求A∩B为:intersect(A,B)

setdiff补集运算

  • 01

    setdiff Set difference. C = setdiff(A,B) for vectors A and B, returns the values in A that are not in B with no repetitions. C will be sorted. C = setdiff(A,B,'rows') for matrices A and B with the same number of columns, returns the rows from A that are not in B. The rows of the matrix C will be in sorted order. [C,IA] = setdiff(A,B) also returns an index vector IA such that C = A(IA). If there are repeated values in A that are not in B, then the index of the last occurrence of each repeated value is returned. [C,IA] = setdiff(A,B,'rows') also returns an index vector IA such that C = A(IA,:). [C,IA] = setdiff(A,B,'stable') for arrays A and B, returns the values of C in the order that they appear in A. [C,IA] = setdiff(A,B,'sorted') returns the values of C in sorted order. If A is a row vector, then C will be a row vector as well, otherwise C will be a column vector. IA is a column vector. If there are repeated values in A that are not in B, then the index of the first occurrence of each repeated value is returned. [C,IA] = setdiff(A,B,'rows','stable') returns the rows of C in the same order that they appear in A. [C,IA] = setdiff(A,B,'rows','sorted') returns the rows of C in sorted order. In a future release, the behavior of the following syntaxes will change including: - occurrence of indices in IA will switch from last to first - orientation of vector C - IA will always be a column index vector - tighter restrictions on combinations of classes In order to see what impact those changes will have on your code, use: [C,IA] = setdiff(A,B,'R2012a') [C,IA] = setdiff(A,B,'rows','R2012a')

  • 02

    例如,求上例中在集合A中,不在集合B中的元素。命令为setdiff(A,B)

    setxor去两个集合交集的补集

    • 01

      例如setxor(A,B)= setxor(A,B) ans = 1     2     3     4     6     8    10    11    13    15

      union取两者的并集

      • 01

        union(A,B)= > union(A,B) ans = 1     2     3     4     5     6     7     8     9    10    11    13    15

      (0)

      相关推荐

      • 利用Matlab控制摄像头进行拍照和监控

        利用Matlab进行摄像头的控制,并用其进行拍照及实时监控等操作 具体操作 01 打开Matlab软件 02 在命令窗口中输入 edit Camera 会打开代码编辑窗口 03 复制如下语句到上面窗口 ...

      • 如何利用MATLAB对图片进行灰度化

        这一篇主要是利用Matlab中的自带函数库中的函数,对图片进行灰度化处理. 操作方法 01 在电脑上安装上Matlab,根据你的需要和电脑的配置安装上相应版本的Matlab,比如我电脑上装的是32位的 ...

      • 如何利用matlab画柱状图

        matlab不仅可以帮助我们生成矩阵,同时也可以生成柱状图,那么如何利用matlab画柱状图呢?下面小编给大家分享一下. 操作方法 01 首先打开matlab软件声明一个X坐标轴的数据,如下图所示 0 ...

      • 利用matlab画100宫格颜色梯度的图像

        宫格里面最著名的莫过于九宫格,他在好多方面都有应用.例如:"九宫格"是我国书法史上临帖写仿的一种界格,又叫"九方格":另外也指一种手机键盘布局,是相对于全键盘而 ...

      • 新手如何利用matlab软件进行简单的矩阵运算

        有时候我们需要进行数据分析,但是对于复杂的矩阵运算的时候,我们往往会计算很长时间,这不仅仅是浪费我们的时间,而且计算量也特别的大.为了解决这个问题,我们利用数学软件matlab来进行简单的操作,复杂的 ...

      • 如何利用MATLAB/SIMULINK搭建简单的仿真模型

        MATLAB/SIMULINK被广泛应用于系统仿真.控制系统设计等领域,SIMULINK中包含很多不同功能的模块库,可以利用其中的模块搭建系统模型,进行系统仿真,使其变得简单而高效,下面简单介绍一下其 ...

      • 如何利用matlab对图片进行读入

        下面介绍如何使用matlab对图片进行读入 操作方法 01 首先打开matlab程序,如下图1所示.并打开文件资源管理器中所要打开图片的位置,如下图2所示. 02 记下图片的宏命令位置,右键属性,比如 ...

      • 如何利用matlab进行图像处理

        matlab不仅有数学处理能力,还具有图像处理功能,那么如何使用呢?先进行一个读取显示的介绍. 操作方法 01 首先,找到matlab 02 然后进入matlab界面 03 输入clear清除左侧 0 ...

      • 如何利用MATLAB求矩阵的伴随阵?

        操作方法 01 3 5 7 例如,求矩阵A=( 5 3 7 )的伴随阵. 6 9 2 02 在MATLAB中输入矩阵A:A=[3 5 7;5 3 7;6 9 2],回车: 03 输入:inv(A)*d ...