当前位置:网站首页>Matlab矩阵(数组)元素过滤常见方法详解
Matlab矩阵(数组)元素过滤常见方法详解
2022-08-11 01:43:00 【撼山拔月】
一、引言
在使用Matlab矩阵或者数组时,有时需要对部分元素进行过滤,也就是把满足某些条件的元素替换为其它数据。本文针对矩阵(数组)元素过滤及应用给出了几种比较简单的方法。
二、矩阵(数组)元素过滤方法
1、利用find
示例1:获取矩阵中满足一定条件的元素组成新的数组。
a = [ -1, 2; 3, -1 ]
b = a( find(a > 0) )
则有输出:
a =
-1 2
3 -1
b =
3
2
此时b是一列向量。由于matlab存储矩阵是按照列方向存储的,所以3在前2在后。
2、利用逻辑表达式
示例2:把矩阵中大于零的元素置为无穷大,其它元素按照某个运算法则更改。
a = [ -1, 2; 3, -1 ]
b( a > 0 ) = Inf;
b( a <= 0 ) = a( a <= 0 ).^2 + 1;
b %此时b是列向量
输出结果:
a =
-1 2
3 -1
b =
2
Inf
Inf
2
如果希望b和a同维,则可以有如下两种方法:
1)方法一:先生成一个与a同维的全零矩阵
a = [ -1, 1; 1, -1; -1, 1 ];
b= zeros( size(a) );
b( a > 0 ) = Inf;
b( a <= 0 ) = a( a <= 0 ).^2 + 1;
b %此时b是和a同维的数组
输出结果:
b =
2 Inf
Inf 2
2 Inf
2)方法二:利用reshape更改列向量为矩阵
a = [ -1, 1; 1, -1; -1, 1 ];
b( a > 0 ) = Inf;
b( a <= 0 ) = a( a <= 0 ).^2 + 1;
b = reshape( b, size(a) )
输出结果:
b =
2 Inf
Inf 2
2 Inf
3、利用逻辑与
示例3:如果把矩阵的某一行或者某一列更改为其它值,操作很简单,例如把矩阵B的第一列置为0,可以如下操作:
B = [ 1, -1; 2, -2; -3, 3 ]
C = B;
C( :, 1 ) = 0
输出结果:
B =
1 -1
2 -2
-3 3
C =
0 -1
0 -2
0 3
示例4:如果要把某些指定位置的元素置为0,则可以类似于C语言中的按位与的操作来执行。先指定一个目标位置非0的矩阵,然后与给定矩阵做逻辑与,之后再点乘给定矩阵
B = [ 1, -1; 2, -2; -3, 3 ];
E = [ 1,1; 0,0; 1,0 ] %逻辑矩阵
D = (B & E) .* B
输出结果:
E =
1 1
0 0
1 0
D =
1 -1
0 0
-3 0
三、矩阵(数组)元素过滤简单应用
1、分段函数求值
示例5:简单分段函数
参考代码:
t = [ -1 : 0.1 : 1 ] * pi;
y( t <= 0 ) = -t( t <= 0 );
y( t > 0 ) = sin( t( t > 0 ) ).*2 + t( t > 0 );
plot( t, y )
运行结果:
示例6:复杂一点的分段函数
参考代码:
x = [ 1 : 20 ];
y( x >= 1 & x <= 5 ) = 6 - x(x >= 1 & x <= 5 );
y( x >= 6 & x <= 15 ) = 1;
y( x >= 16 & x <= 20 ) = x( x >= 16 & x <= 20 ) - 15;
xlen = length( x )
ylen = length( y )
plot( x, y, 'b' );
axis( [ 0, 21, 0, 6 ] );
运行结果:
2、利用已知的数组过滤其它数组
示例7:
a = [ -1, 1; 1, -1 ]
b = pascal( 2 )
b( a > 0 ) = 5
b( a+3 < b ) = 3
b( b < 2 ) = b( b < 2 ) + 2
运行结果:
a =
-1 1
1 -1
b =
1 1
1 2
b =
1 5
5 2
b =
1 3
3 2
b =
3 3
3 2
边栏推荐
- 【Video】Report Sharing | 2021 Insurance Industry Digital Insights
- Ambari迁移Spark2到其它机器(图文教程)
- 颠覆性创新招商,链动2+1是个怎么样的制度模式?
- 阿里亿级并发册 + 机器学习算法 + 面试册 + 优化册 + 代码册 笔记!!!
- 【服务器数据恢复】raid5崩溃导致lvm信息和VXFS文件系统损坏的数据恢复案例
- 从键入网址到网页显示的详细过程
- [Server data recovery] Data recovery case of lvm information and VXFS file system corruption caused by raid5 crash
- Alibaba 最新神作!耗时 182 天肝出来 1015 页分布式全栈手册太香了
- 软件测试面试题:什么是α测试,β测试?
- Qt 中的隐式共享
猜你喜欢

Two-dimensional array combat project -------- "Minesweeper Game"

Mysql database installation and configuration detailed tutorial

MySQL八股文背诵版(续)

络达开发---自定义Timer的实现

The latest domestic power supply manufacturers and pin-to-pin replacement manuals for specific models are released

MySQL - 一条SQL在MySQL中是如何被执行的?

年薪30W,BAT抢着要,懂面试技巧的测试人究竟多吃香?

最新国产电源厂家及具体型号pin-to-pin替代手册发布

88Q2110 access C45 phy address through C22

Update chromedriver driver programming skills │ selenium
随机推荐
软件测试面试题:验收测试包括哪三种类型?
Data Filters in ABP
导入数据包上传宝贝提示“类目不能为空”是什么原因,怎么解决?
【Video】Report Sharing | 2021 Insurance Industry Digital Insights
安装dlib库
MySQL indexes and transactions
Is container technology really the savior of environmental management?
How to determine the size of the version number
Ambari Migrates Spark2 to Other Machines (Graphic and Text Tutorial)
vim简单保存窗口标识
惨遭面试官吊打高并发系统设计,回来学习 2400 小时后成功复仇
Summary of DDL routine operations in MySQL
sql 使用到where和groupby时到底怎么建立索引?
SQL statement--get database table information, table name, column name, description comment, etc.
loop word
zerorpc:async=True can be written as **{“async“: True}
install dlib library
【服务器数据恢复】raid5崩溃导致lvm信息和VXFS文件系统损坏的数据恢复案例
测试3年,开口就要25k?面试完最多给15k...
Mysql database installation and configuration detailed tutorial