Matlab,替代创建一个额外的向量? [英] Matlab, alternative to creating an extra vector?

查看:24
本文介绍了Matlab,替代创建一个额外的向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这个我得到一个错误

sum(vector) == cumsum(vector)(length(vector))>>错误:()-索引必须出现在索引表达式的最后.

我知道我能做到:

Vec1 = cumsum(mat);总和(垫子)== Vec1(长度(垫子))

这将返回一个逻辑 1.

有没有其他方法可以将所有内容放在一行上?

解决方案

好吧,如果你绝对下定决心要一口气搞定,

sum(vec) == subsref(cumsum(vec),struct('type','()','subs',{{numel(vec)}}))

但这是对 subsref,一般用于重载自定义类中的下标运算符(即{}().).p>

If I have this I get an error

sum(vector) == cumsum(vector)(length(vector))

>> Error: ()-indexing must appear last in an index expression.

I know I can just do:

Vec1 = cumsum(mat);
sum(mat) == Vec1(length(mat))

which will return a logical 1.

Is there an alternative to get everything on a single line?

解决方案

Well, if you are absolutely determined to do it in one line,

sum(vec) == subsref(cumsum(vec),struct('type','()','subs',{{numel(vec)}}))

But this is a borderline abuse of subsref, which is generally used for overloading the subscripting operators (i.e. {}, (), .) in custom classes.

这篇关于Matlab,替代创建一个额外的向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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