Matlab中对逻辑矩阵表达式的所有条目求和的表达式?
将矩阵中的所有元素相加,您通常会这样做
A = sum ( B(:) );
,这很好而且很短。然而,假设我们有一个像这样的逻辑表达式
B = B == 6
,并且我们想要对所有条目的元素求和,那么最聪明的方法似乎是这样做
A = sum ( sum ( B == 6 ) )
,或者
B = B == 6;
A = sum( B(:) );
两者都有点丑陋。所以我想知道有没有更好的表达方式?
A = sum ( (B == 6)(:) );
会很好,但不起作用。
To sum all the elements in a matrix you usually do
A = sum ( B(:) );
which is nice and short. However assume that we have a logical expression like this
B = B == 6
and we want to sum the elements of all the entries, then smartest way seems to be to do
A = sum ( sum ( B == 6 ) )
or
B = B == 6;
A = sum( B(:) );
Both are kind of ugly. So I was wondering is there a nicer expression?
A = sum ( (B == 6)(:) );
Would be nice but doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么这个简单的解决方案有什么令人讨厌的......
So what is so nasty about the simple solution...
并不是我建议这样做,而是如图所示 以前,您实际上可以执行以下操作:
Not that I recommend this, but as was shown previously, you can actually do something like: