Equality results when using a matrix of strings

僤鯓⒐⒋嵵緔 提交于 2021-02-10 12:56:03

问题


I don't understand why using == to compare between a string vector and a matrix of strings and the vector is of dimension n, I obtain a matrix of size n * n. I expected it to be of size n only, with a 1 when the string is equal.

octave:13> t = ["aha";"bgb";"ctc"]
t =

aha
bgb
ctc

octave:14> t == "aha"
warning: mx_el_eq: automatic broadcasting operation applied
ans =

   1   1   1
   0   0   0
   0   0   0

What's going on underneath to explain such result ? And is it possible to do away with the warning: warning: mx_el_eq: automatic broadcasting operation applied ?

For example, with a vector of integers, it behave like I expect:

octave:16> t2 = [1,2,2,3,4]
t2 =

   1   2   2   3   4

octave:17> t2 == 4
ans =

   0   0   0   0   1

回答1:


What is actually happening is that you are creating a 3 x 3 matrix of characters.

Observe when you determine the class of the matrix as well as the corresponding number of rows and columns in the matrix:

octave:1> t = ["aha";"bgb";"ctc"]
t =

aha
bgb
ctc

octave:2> class(t)
ans = char
octave:3> size(t)
ans =

   3   3

When you do t == "aha", normally when you perform logical operations, both the left hand and right hand side of the expression should match in size. The size of each variable matches in size because an element-wise comparison is performed. However, because "aha" is a 3 x 1 character array and your array t is 3 x 3, Octave will automatically broadcast so that the comparison matches in size. This means that "aha" will be transformed into a 3 x 3 matrix where you see the string "aha" in each row. This is the reason why you get a 3 x 3 logical matrix where the first row is all true because you are comparing characters on an individual basis and it turns out that the first row of t matches character by character with the transformed "aha" matrix that was duplicated along the rows three times. You get this warning in Octave but in MATLAB, you get an error immediately because the dimensions don't match in size.

This is what happens in MATLAB. Note that strings are created using the single quotation, not the double quotation.

>> t = ['aha';'bgb';'ctc']

t =

aha
bgb
ctc

>> t == 'aha'
Error using  == 
Matrix dimensions must agree.

However, you can eliminate the "warning" by performing proper broadcasting by using bsxfun(MATLAB doc, Octave doc). This applies in both platforms.

In Octave:

octave:4> bsxfun(@eq, t, "aha")
ans =

   1   1   1
   0   0   0
   0   0   0

In MATLAB:

>> bsxfun(@eq, t, 'aha')

ans =

     1     1     1
     0     0     0
     0     0     0

For the case of the integer, the single digit 4 is well defined and performing t2 == 4 is implied behaviour so you don't get the warning. We know that we need to compare element-wise with every element in t2 to see if a 4 is matched in each position.


To do what you actually want, horchler made a very nice observation. Convert your matrix of characters into a cell array where each element in this array a string consisting of an entire row of characters in your matrix, then use strcmp between the single string and the cell array of characters. It will return a logical vector of length n that performs what you want:

octave:5> strcmp(mat2cell(t,ones(1,size(t,1)),size(t,2)),'aha')
ans =

   1
   0
   0

The function mat2cell (MATLAB doc, Octave doc) will transform a matrix into a cell array. What we're doing here is determining how to populate the cell array. We are saying that the contents of each cell array should be 1 x size(t,2) where size(t,2) is the total number of columns in t. We then use strcmp (MATLAB doc, Octave doc) to compare each string in this cell array with the string "aha". What is returned is the logical vector that you expect.

Even simpler is to create a cell array of strings immediately, then just perform strcmp on this cell array:

octave:6> t = {"aha"; "bgb"; "ctc"}
t = 
{
  [1,1] = aha
  [2,1] = bgb
  [3,1] = ctc
}
octave:7> strcmp(t,"aha")
ans =

   1
   0
   0


来源:https://stackoverflow.com/questions/35976243/equality-results-when-using-a-matrix-of-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!