在Matlab中创建此矩阵 [英] creating this matrix in matlab

查看:72
本文介绍了在Matlab中创建此矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出

A=[a_1 a_2 a_3 ... a_n]

如何制作?

[a1 ... a100]
[a2 ... a101]
...
[an-100+1 ... an]

我不想在这里使用for循环,因为我想加快速度. 谢谢.

I want to not use for-loop here since I want to speed it up. Thank you.

推荐答案

您可以使用:

n = numel(A);
m = 100;
I = bsxfun(@plus, 1:m, (0:n-m).');
B = A(I);

作为旁注:for循环执行的不好:

As a sidenote: The for loop doesn't perform that bad:

B = zeros(n-m+1, m);
for i = 1:size(B)
    B(i,:) = A(i:i+m-1);
end

就我的测试而言,它仅慢4倍,而且无论如何,此计算都几乎不会成为您程序中的瓶颈.

As far as my testing goes, it is slower only by a factor of 4 and this calculation should hardly be a bottleneck in your program anyway.

这篇关于在Matlab中创建此矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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