MATLAB 中的二元运算
我有两个向量 A = [1 0 0 0 0 1] 和 B = [1 0 0 1 0 1]。我想从matlab中的向量计算 (1,1) (1,0) (0,1) 和 (0,0) 的数量。知道如何搭配它。
I have two vectors A = [1 0 0 0 0 1] and B = [1 0 0 1 0 1]. I want to calculate the number of (1,1) (1,0) (0,1) and (0,0) from the vectors in matlab. Any idea how to go with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否考虑过阅读 Matlab 教程?您可能比等待答案出现在这里更快地找到答案。
无论如何,用于逻辑与的 matlab 运算符是
&
,用于逻辑非的运算符是~
,并且两者都适用于double
向量和矩阵(即按照您的问题定义时 A 和 B 的默认类型;所有非零值将被视为1
)。完成所需的连接后,如果 x 的类型为逻辑类型或逻辑类型,则 sum(x) 将为您提供 x 中的个数。
double
仅包含0
和1
。粗简形式:
Did you consider reading a Matlab tutorial? You might have found an answer faster than waiting for it to appear here.
Anyway, the matlab operator for logical AND is
&
, and the one for logical negation is~
, and both work also ondouble
vectors and matrices (i.e. the default type of which A and B are when defined as in your question; all non-zero values values will be treated like1
s).Once you have made the required connection,
sum(x)
will give you the number of ones inx
if x is of typelogical
or adouble
with only0
s and1
s.Crude short form: