How do I generate a vector of ordered integer
1 次查看(过去 30 天)
显示 更早的评论
I want to generate a vector of the form [1 2 3 1 2 3 4 5 1 2 3 4 5 6 7 1 2 3 4 5 6 7 9] with simple matlab code.
0 个评论
采纳的回答
the cyclist
2015-11-21
编辑:the cyclist
2015-11-21
One way:
x = [1 2 3 1 2 3 4 5 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9];
But I'm guessing that's not what you meant.
Another way:
x = [];
for k = 3:2:9
x = [x, 1:k]
end
In general, it is not a good idea to "grow" a vector like this, by simply appending. It's better to define the vector length ahead of time, preallocating the memory. But, I'm feeling lazy right now.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!