Find cell strings inside commonly used in three ways.
method one
>> C = {1,5,3,4,2,3,4,5,2,1};
index = find([C{:}] == 5)
index =
2 8
First, by a method [C {:}] operation, the elements within the cell into an array, then the array performs find () function returns the index to find the string.
And numeric data types simultaneously in a single character within the search cell
This method is only a number (including decimal) or a single char (single character) effective.
If more than one character action following error:
>> c={'1' '5d' '3' '4' '2' '3' '4' '5' '2' '1'};
>> index = find([c{:}] == '5d')
错误使用 ==
矩阵维度必须一致。
The reason why the following error occurs, the key is [] operation .
Let's take a look at [] the difference between operating on different types of data
- A plurality of numeric type variables that are [] operations, essentially all of the variables into an array in the system returns a numeric array;
>> C = {1,5,3,4,2,3,4,5,2,1};
[C{:}]
ans =
1 5 3 4 2 3 4 5 2 1
- A plurality of character variables, performing [] on the operation is essentially performed a string concatenation , it returns a char array , each array element storing a character length of 1
>> c={'1' '5d' '3' '4' '2' '3' '4' '5' '2' '1'};
[c{:}],class([c{:}])
ans =
15d34234521
ans =
char
In addition, the study found, find: the right length statement ([c {}] == ' 5d') per unit length must be left in the array
so back to the beginning of the problem statement find ([c {:}] = = '5D') , the left side of the equal sign is the n-1 * character array, an array of characters to the right of the * 2, 2 is not equal to the length of the right side of the left side of the minimum unit 1, the error: error matrix using == dimension must be consistent.
Method Two
>> c={'1' '5d' '3' '4' '2' '3' '4' '5' '2' '1'};
index = find(strcmp(c,'5d'))
index =
2
strcmp () method capable of simultaneously retrieving a character and the string
strcmp () method can not retrieve numeric data.
>> c={1,'5',3,4,2,3,4,5,2,1};index = find(strcmp(c,5))
index =
空矩阵: 1×0
Method Three
>> c={'1' '5d' '3' '4' '2' '3' '4' '5' '2' '1'};
>index = find(ismember(c,'5d'))
index =
2
You can retrieve character string and a single character
Can not be retrieved numeric data types,
can not contain mixed data types Cell (or contains numeric and character string) be retrieved.
Slower
>> c={1,'5d','5',4,2,3,4,5,2,1};
idx = find(ismember(c,'5'))
错误使用 cell/ismember (line 34)
类 cell 的输入 A 和类 char 的输入 B 必须为字符串元胞数组,除非其中某个输入为字符串。
to sum up
By table summarize the three methods Scope
method | Retrieving data cell contains a mixture of | Retrieving numeric | Retrieving a single character | Retrieving string |
---|---|---|---|---|
method one | √ | √ | √ | × |
Method Two | √ | × | √ | √ |
Method Three | × | × | √ | √ |