7/11/2023 0 Comments Sorted vector code matlabThere is actually reference to an ISMEMBERLAST builtin, but it doesn't seem to exist (yet?). % instances occur, the first occurence is returned. % locations of the elements of A occurring in B. % ISMEMBERFIRST(A,B) - B must be sorted - Returns a vector of the Here are the bits of documentation for these functions inside ismember.m: % ISMEMBC2 - S must be sorted - Returns a vector of the locations of % lastInd = ismembc2(searchfor,x(firstInd:end)) firstInd-1 % slower LastInd = ismembc2(searchfor,x) % find last occurrence However, if you have a recent version of MATLAB (R2012b , I think), you can use yet more undocumented builtin functions to get the first an last indexes! These are ismembc2 and builtin('_ismemberfirst',searchfor,x): firstInd = builtin('_ismemberfirst',searchfor,x) % find first occurrence As the OP points out, it is unfortunate that =ismember(3,x) only provides the location of the first occurrence of 3 in x, rather than all. are somewhat backwards (searching for each element of x in the second argument rather than the other way around), the code is much slower than necessary. Since the above applications of ismember, etc. Note that newer versions of matlab have a builtin called by builtin('_ismemberoneoutput',a,b) with the same functionality. Using ismembc will save computation time since ismember calls issorted first, but this will omit the check. Note that there is a helper function used by ismember that you can call directly: % ISMEMBC - S must be sorted - Returns logical vector indicating which You just need to use the right order of inputs. Ismember will give you all the indexes if you look at the first output: > x = = myFindDrGar(x,value,value) ĮDIT many years later: there was an error in the last two if/else blocks, fixed it. Note that the improved version of Daniels searchFor function is now simply: function = mySearchForDrGar(x,value) Lower_index_b=up ?crease lower_index_b (whose x value remains greater than upper bound and thus lower bound) If (lw>upper_index_a)
0 Comments
Leave a Reply. |