根据函数分配Matlab向量 [英] Assigning a Matlab vector according to a function

查看:99
本文介绍了根据函数分配Matlab向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想向量化以下内容:

I basically want to vectorize the following:

vect_y = zeros(1,numel(vect_x);
for i = 1:numel(vect_x)
    vect_y = sum(vect_x(1:i));
end

这可能吗?例如,我尝试通过以下方式使用arrayfun:

Is this possible? As an example, I was trying to use arrayfun the following way:

y = arrayfun(@(y) sum(y), vect_x(1:1), vect_x(1:2), ..., vect_x(1:n));

但是这行不通,也不干净.

But this won't work and it's not clean.

因此,我现在知道cumsum可以解决上述问题,但是我对如何对任何功能执行此操作感到好奇.

edit: So I know now that cumsum solves the above, but I am curious as to how I would do this for any function.

推荐答案

您可以使用 cumsum 直接运行:

What you want can be done with the cumsum function directly:

vect_y = cumsum(vect_x);

这篇关于根据函数分配Matlab向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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