creating this matrix in matlab

浪子不回头ぞ 提交于 2020-01-06 18:00:29

问题


Given

A=[a_1 a_2 a_3 ... a_n]

How to make this?

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

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


回答1:


You can use:

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

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

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.



来源:https://stackoverflow.com/questions/29588042/creating-this-matrix-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!