Analysis of Matlab # # # # nature using a variety of methods to find the index of the specified string cellular (cell) and analyzed the limitations and pitfalls

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.

  • advantage

And numeric data types simultaneously in a single character within the search cell

  • limitation

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')
错误使用  == 
矩阵维度必须一致。
  • Depth analysis of the reasons (trap) errors

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

  1. 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
  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
  • advantage

strcmp () method capable of simultaneously retrieving a character and the string

  • limitation

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
  • advantage

You can retrieve character string and a single character

  • limitation

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 × ×
Published 59 original articles · won praise 2 · Views 4636

Guess you like

Origin blog.csdn.net/lch551218/article/details/103805042