Create a matrix from a row vector according to specifications

1 次查看(过去 30 天)
I am working in matlab. I have a row vector in and a scalar number fuzzy_no. I want to create a matrix output of size fuzzy_no times (numel(in)-fuzzy_no). such that the ith col of the matrix output has the elements from i:i+fuzzy_no-1 of row vector in.
In other words I want to implement the following loop without using loops
n = numel(in);
output = zeros(fuzzy_no,n-fuzzy_no);
for i = 1:size(output,2)
output(:,i) = in(1,i:i+fuzzy_no-1);
end

采纳的回答

Andrei Bobrov
Andrei Bobrov 2014-5-30
编辑:Andrei Bobrov 2014-5-30
output = hankel(in(1:n-fuzzy_no),in(n-fuzzy_no:end-1))';
or
output = in(bsxfun(@plus,1:n-fuzzy_no,(0:fuzzy_no-1)'));
  1 个评论
Nishant
Nishant 2014-5-30
Thanks, I needed the last element to be missing so this worked fine (without the transpose),
output = hankel(in(1:n-fuzzy_no),in(n-fuzzy_no:end-1));

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by