简体   繁体   中英

creating this matrix in matlab

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM