Avoid for loop?
1 次查看(过去 30 天)
显示 更早的评论
I want to generate arbitrary matrix of indeces, e.g.:
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
6 7 8 9
One can observe a series in it. I solved the problem as follows:
% --- Inputs
x = 1:9;
window = 4;
% --- creating the matrix
mm = length(x);
nb_rws = mm - window + 1;
y = ones(nb_rws,window);
for i = 1 : nb_rws
y(i,:) = x(i : i + window -1);
end
y
Is there a solution to avoid the for loop? Thanks..
0 个评论
采纳的回答
madhan ravi
2018-12-19
编辑:madhan ravi
2018-12-19
y=hankel((1:4),(4:9))'
Gives:
y =
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
6 7 8 9
0 个评论
更多回答(1 个)
Stephen23
2018-12-19
Simpler:
>> hankel(1:6,6:9)
ans =
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
6 7 8 9
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!