当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
初识Opencv4.X----均值滤波
Definition of cell
~1 CCF 2022-06-2 treasure hunt! Big adventure!
Create personal extreme writing process - reprint
换电脑后如何配置SSH
[code source] daily one question non decreasing 01 sequence
Student management system (summary)
Dream set sail (the first blog)
初识Opencv4.X----方框滤波
Flex 布局语法与用例
*6-1 CCF 2015-03-2 numerical sorting
How many regions can a positive odd polygon be divided into
[code source] add brackets to the daily question
初识Opencv4.X----为图像添加椒盐噪声
Redis list 结构命令
Use kotlin use to simplify file reading and writing
Browser access to swagger failed with error err_ UNSAFE_ PORT
[code source] daily question farmland Division
降低程序空间复杂度的一些技巧
UI原型资源









