当前位置:网站首页>Detailed explanation of common methods of filtering matrix (array) elements in Matlab
Detailed explanation of common methods of filtering matrix (array) elements in Matlab
2022-08-11 01:55:00 【Shaking the Mountain and the Moon】
I. Introduction
When using Matlab matrices or arrays, it is sometimes necessary to filter some elements, that is, to replace elements that meet certain conditions with other data.In this paper, several relatively simple methods are given for the filtering and application of matrix (array) elements.
Second, matrix (array) element filtering method
1. Use find
Example 1: Get elements in the matrix that meet certain conditions to form a new array.
a = [ -1, 2; 3, -1 ]b = a( find(a > 0) )
There is an output:
a =
-1 2
3 -1
b =
3
2
At this point b is a column vector.Since the matlab storage matrix is stored in the column direction, 3 is in the front and 2 is in the back.
2. Use logical expressions
Example 2: Set the elements greater than zero in the matrix to infinity, and other elements are changed according to a certain algorithm.
a = [ -1, 2; 3, -1 ]b( a > 0 ) = Inf;b( a <= 0 ) = a( a <= 0 ).^2 + 1;b % Now b is a column vector
Output result:
a =
-1 2
3 -1
b =
2
Inf
Inf
2
If you want bIf it has the same dimension as a, there are two methods as follows:
1) Method 1: First generate an all-zero matrix with the same dimension as 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 % At this time, b is an array of the same dimension as a
Output result:
b =
2 Inf
Inf 2
2 Inf
2) Method 2: Use reshape to change the column vector to a matrix
a = [ -1, 1; 1, -1; -1, 1 ];b( a > 0 ) = Inf;b( a <= 0 ) = a( a <= 0 ).^2 + 1;b = reshape( b, size(a) )
Output result:
b =
2 Inf
Inf 2
2 Inf
3, using logical AND
Example 3: If you change a row or column of the matrix to another value, the operation is very simple. For example, setting the first column of matrix B to 0, you can do the following:
B = [ 1, -1; 2, -2; -3, 3 ]C = B;C( :, 1 ) = 0
Output result:
B =
1 -1
2 -2
-3 3
C =
0 -1
0 -2
03
Example 4: If you want to set some elements at specified positions to 0, you can perform a bitwise AND operation similar to the C language.First specify a matrix with a non-zero target position, then do a logical AND with the given matrix, and then dot multiply the given matrix
B = [ 1, -1; 2, -2; -3, 3 ];E = [ 1,1; 0,0; 1,0 ] % logical matrixD = (B & E) .* B
Output result:
E =
1 1
0 0
1 0
D =
1 -1
0 0
-3 0p>
Three, simple application of matrix (array) element filtering
1. Piecewise function evaluation
Example 5: Simple piecewise function
Reference code:
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 )
Run result:
Example 6: A bit more complicated piecewise function
Reference code:
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 ] );
Run result:
2. Use a known array to filter other arrays
Example 7:
a = [ -1, 1; 1, -1 ]b = pascal( 2 )b( a > 0 ) = 5b( a+3 < b ) = 3b( b < 2 ) = b( b < 2 ) + 2
Running result:
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
边栏推荐
猜你喜欢
#yyds Dry Goods Inventory#[Yugong Series] August 2022 Go Teaching Course 008-Integer of Data Types
漏洞管理计划的未来趋势
The latest domestic power supply manufacturers and pin-to-pin replacement manuals for specific models are released
如何防止离职员工把企业文件拷贝带走?法律+技术,4步走
MySQL进阶查询
连流量染色都没有,你说要搞微服务?
联盛德W801系列6-从微信小程序的角度来分析W801的蓝牙通信源码(indicate方式)
22、库存服务
【开源】壁纸软件,给自己电脑设计专属特效
【iframe父页面调用子页面的方法】踩坑:获取元素的时候需要用 `[x]`是关键,不能用`.eq(x)`否则获取不到。
随机推荐
Ora - 00001 in violation of the only constraint
vim取上个窗口号.
MySQL八股文背诵版(续)
wincc如何实现远程监控1200PLC
络达开发---自定义BLE服务(二):功能实现
软件测试面试题:软件测试的过程的V模型,说出它的缺点?
0图中等 LeetCode565. 数组嵌套
【HFSS学习记录2】腔体滤波器的设计与仿真
Oops Framework模板项目新手引导
项目构建工具-Gradle入门
【websocket】
vim简单保存窗口标识
std::format格式化自定义类型
How to convert url to obj or obj to url
C# string与stream的相互转换
【微波工程学习记录1】功率分配器和定向耦合器
【服务器数据恢复】raid5崩溃导致lvm信息和VXFS文件系统损坏的数据恢复案例
QT+VTK+PCL拟合圆柱并计算起始点、中止点
软件测试面试题:I P协议、RARP协议、ICMP协议与ARP协议的功能是什么?
paddle2.3和torch1.8在SentenceBert上的性能对比