当前位置:网站首页>Some usages of Matlab's find() function (quickly find qualified values)
Some usages of Matlab's find() function (quickly find qualified values)
2022-07-25 09:45:00 【Frost Creek】
Sometimes , There's a set of data , And the task , It requires us to find the position of data that conforms to certain rules in this group of data , such as Greater than a number , Or is it Equal to one Count , Or a multiple of a certain number, and so on . Such problems sometimes make us feel headache . Now? , Let's think of a way , So that this problem can be well solved .
For this kind of problem , One of the simplest , Write a Loop traversal , Find eligible data , Make sure that Location ( Index value ), Then save the location data . you 're right , It's very simple , Next, we will implement this method .
The problem background : One set of data is [1,2,3,4,0,1,0,10,12,16], Of course , Maybe more , Find something that can be 2 Divisible data , And record its location .
clc;clear;M=[1,2,3,4,0,1,0,10,12,16];n=length(M);List=[];j=0;for i=1:nif mod(M(i),2)==0 % Determine whether it can be 2 to be divisible byj=j+1;List(j)=i;endenddisp(List)% Where to output qualified dataRE=M(List);% Get qualified datadisp(RE)% Output qualified data
result :2 4 5 7 8 9 10 % Location of eligible data2 4 0 0 10 12 16 % Eligible data
Of course , This can solve the problem well . however , But it's a little complicated , Is there a simpler way ? ok , It does .
Form 1 :find( Judge the condition )
M=[1,2,3,4,0,1,0,10,12,16];r1=find(M==0); % Data found 0 Position of elementsdisp(r1);r2=find(M~=0);% Find the non 0 Position of elementsdisp(r2);r3=find(M==1);% Find a value equal to ( Here for 1) The location of the elements ofdisp(r3);r4=find(M>10);% Find a value greater than ( Here for 10) The location of all the elements ofdisp(r4);r5=find(mod(M,2)==0);% Find something that can be 2 The position of all elements divisibledisp(r5);result :5 71 2 3 4 6 8 9 101 69 102 4 5 7 8 9 10
Of course , This judgment condition can be many, many , You can set it according to your actual needs .
Form 2 ( Main operation and 0 of ):
1 index=find(M)
Find the data M Position of all non-zero elements in ( Index value ), The return value is the location label .M It can be a vector , It can also be a matrix .
>> M=[1,2,3,4,6,0,0,9,0];>> find(M)ans =1 2 3 4 5 8%-------------------------------------->> M=[1,2;0,8];>> find(M)ans =134notes :matlab The elements of the matrix are stored by column
2 index=find(M,n)
Return to the former n The position of a non-zero element ,M It can be a vector , It can also be a matrix .
>> M=[1,2,3,4,1,6,0,0,9,0];>> find(M,1)ans =1>> find(M,2)ans =1 2>> find(M,0)Misuse findThe second argument must be a positive scalar integer .
3 index=find(M,1,'last')
Returns the position of the last non-zero element ,M It can be a vector , It can also be a matrix .
>> M=[1,2,3,4,1,6,0,0,9,0];>> find(M,1,'last')ans =9
notes :matlab The elements of the matrix are stored by column
Form 3 ( Mainly for matrix )
Through the operation in form 2 , For matrices , We got the position , However, this position , Computers can recognize , But for us, we have to do some calculations . such as , One 2 Order matrix . Start with the first in the first column , Store... In columns , The serial numbers are 1,2,3,4. In fact, if you use two numbers to determine , Then the corresponding relationship is like this
1-----------(1,1)
2-----------(2,1)
3-----------(1,2)
4-----------(2,2)
[r,c]=find( Judge the condition )
r: Row index
c: Column index
example :
M=[1,2,3;4,0,8;0,0,9];[r,c]=find(M~=0);disp(r);disp(c);disp([r,c]);result :1211231123331 12 11 21 32 33 3
obviously , Use find() Function can make our programming very simple , It will greatly reduce our programming time , So as to leave us more time to think about more complex problems .
边栏推荐
- 打造个人极限写作流程 -转载
- Raspberry sect door ban system based on face recognition
- ~1 CCF 2022-06-2 treasure hunt! Big adventure!
- How to configure SSH after changing the computer
- Redis list structure command
- ¥ 1-3 SWUST OJ 942: reverse sequence table
- Install MySQL in Ubuntu and create new users
- Learning new technology language process
- Save pdf2image PDF file as JPG nodejs implementation
- 2022年的个人技术选型梳理
猜你喜欢
随机推荐
OC--对象复制
SurfaceView 闪屏(黑一下问题)
Voice chat app source code - produced by NASS network source code
Wechat applet realizes the rotation map (automatic switching & manual switching)
OC--Foundation--字符串+日期和时间
Flutter rive multi state example
The shortest path problem Bellman Ford (single source shortest path) (illustration)
自定义Dialog 实现 仿网易云音乐的隐私条款声明弹框
初识Opencv4.X----均值滤波
matlab的find()函数的一些用法(快速查找符合条件的值)
Learning new technology language process
OC--初识
The jar package has been launched on Alibaba cloud server and the security group has been opened, but postman still can't run. What should we do
基于stm32的恒功率无线充电
Redis database foundation
Redis installation (Ubuntu)
How many regions can a positive odd polygon be divided into
## 使用 Kotlin USE 简化文件读写
Learn redis Linux and install redis
OC--Foundation--集合









