Fast creation of vector [0 0 1 1 2 2 3 3... n n]
8 次查看(过去 30 天)
显示 更早的评论
Hi all,
like mentioned in the title, is there a fast way of creating a vector with repeating digits?
Thanks!
Simon
0 个评论
采纳的回答
per isakson
2014-6-30
编辑:per isakson
2014-6-30
Test
>> reshape( cat( 1, [0:n], [0:n] ), 1, [] )
ans =
0 0 1 1 2 2 3 3 4 4 5 5
and
>> reshape( repmat( [0:n], 2,1 ), 1, [] )
更多回答(4 个)
Jos (10584)
2014-6-30
n = 10 % max value
k = 3 % number of repetitions
V = floor((0:k*(n+1)-1)/k)
3 个评论
Jos (10584)
2014-7-1
Reshaping does not take a lot of time. Is it also faster for larger values of k?
Danilo NASCIMENTO
2014-6-30
V=zeros(1,20);
k=0;
i=1;
while i<20
V(i)=k;
V(i+1)=k;
k=k+1;
i=i+2;
end
V
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!