使用字符串矩阵时产生相等结果 [英] Equality results when using a matrix of strings

查看:38
本文介绍了使用字符串矩阵时产生相等结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么要使用 == 在字符串向量和字符串矩阵之间进行比较,并且该向量的维数为 n ,我得到一个矩阵大小 n * n .我希望它的大小仅为 n ,当字符串相等时为1.

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

下面是怎么解释这种结果的?是否可以取消警告:warning: mx_el_eq: automatic broadcast operation applied ?

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

推荐答案

实际发生的是您正在创建 3 x 3 字符矩阵.

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

当您执行 t =="aha" 时,通常在执行逻辑运算时,表达式的左手边和右手边都应大小匹配.每个变量的大小在大小上匹配,因为执行了逐元素比较.但是,由于"aha" 是一个 3 x 1 字符数组,而您的数组 t 3 x 3 ,八度会自动广播,以便比较大小匹配.这意味着"aha" 将被转换为3 x 3矩阵,您将在每行中看到字符串"aha" .这就是为什么您得到 3 x 3 逻辑矩阵的原因,其中第一行都是正确的,因为您是在逐个比较字符,结果是 t 将字符与转换后的"aha" 矩阵逐字符匹配,该矩阵沿行重复了三遍.在Octave中会收到此警告,但在MATLAB中,由于尺寸不匹配,会立即收到错误消息.

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.

这就是在MATLAB中发生的情况.请注意,字符串是使用单引号而不是双引号创建的.

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.

但是,您可以通过使用 bsxfun (八度文档).这在两个平台上都适用.

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

在八度音阶中:

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

   1   1   1
   0   0   0
   0   0   0

在MATLAB中:

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

ans =

     1     1     1
     0     0     0
     0     0     0

对于整数,很好地定义了一位数字 4 ,并且执行 t2 == 4 是隐含的行为,因此您不会收到警告.我们知道我们需要将 t2 中的每个元素逐元素进行比较,以查看每个位置是否匹配4.

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.

为了做您真正想要的事情,霍希勒做了一个很好的观察.将您的字符矩阵转换为元胞数组,其中该数组中的每个元素都是由矩阵中的整行字符组成的字符串,然后在单个字符串和字符元胞数组之间使用 strcmp.它将返回长度为 n 的逻辑矢量,该逻辑矢量执行您想要的操作:

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

函数 mat2cell ( MATLAB文档八度doc )会将矩阵转换为单元格阵列.我们在这里所做的就是确定如何填充单元格数组.我们说每个单元格数组的内容应为 1 x size(t,2),其中 size(t,2)中的总列数> t .然后,我们使用 strcmp ( MATLAB文档.返回的是您期望的逻辑向量.

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.

更简单的方法是立即创建一个字符串单元格数组,然后在此单元格数组上执行 strcmp :

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

这篇关于使用字符串矩阵时产生相等结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆