ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
index.m
Go to the documentation of this file.
1%> \brief
2%> Return the starting index of the first occurrence
3%> of the input scalar MATLAB string ``pattern`` in
4%> the input scalar MATLAB string ``str``, otherwise,
5%> return ``0`` to indicate the lack of the ``pattern``
6%> in the input ``str``.<br>
7%>
8%> \details
9%> This function partially replicates the functionality
10%> of the Fortran intrinsic function ``index()``.<br>
11%> This function uses the MATLAB intrinsic ``strfind()``
12%> to achieve the goal. However, unlike ``strfind()``
13%> it always returns a number such that the function
14%> can be directly used in string slicing.<br>
15%>
16%> \param[in] str : The input scalar MATLAB string to be searched
17%> for the presence of the input pattern.<br>
18%> \param[in] pattern : The input scalar MATLAB string to be
19%> searched for within the input ``str``.<br>
20%>
21%> \return
22%> ``loc`` : The output scalar MATLAB integer
23%> containing the location of the first occurrence of the
24%> input ``pattern`` in the input ``str`` or ``0`` if no such
25%> pattern exists.<br>
26%>
27%> \interface{index}
28%> \code{.m}
29%>
30%> loc = pm.str.index(str, pattern)
31%>
32%> \endcode
33%>
34%> \example{index}
35%> \include{lineno} example/str/index/main.m
36%> \output{index}
37%> \include{lineno} example/str/index/main.out.m
38%>
39%> \final{index}
40%>
41%> \author
42%> \JoshuaOsborne, May 21 2024, 4:34 AM, University of Texas at Arlington<br>
43%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
44%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
45function loc = index(str, pattern)
46 loc = strfind(str, pattern);
47 if isempty(loc)
48 loc = 0;
49 else
50 loc = loc(1);
51 end
52end
function index(in array)
Given array(1 : n), return the array index indx(1 : n) such that array(indx) is in ascending order.