How can I generate a vector like this 1,2,2,3,3,3,4,4,4,4 by using for loop?
5 ビュー (過去 30 日間)
古いコメントを表示
Hi all,
There's a problem that asks to generate a vector like 1,2,2,3,3,3,4,4,4,4
So if the input n = 5, then the output = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
I tried to solve it by using for loop
but I couldn't find a way to do this
function y = your_fcn_name(n)
L = length((n^2+n)/2)
for i = 1:L
end
I don't know what should I do inside the for loop, I tried to get the relation between the value and its index in the vector, but I couldn't
can you help me solve this problem with a for loop?
3 件のコメント
Stephen23
2023 年 8 月 19 日
Not an answer to this question, but perhaps useful to someone:
n = 5;
v = repelem(1:n,1:n)
採用された回答
Torsten
2023 年 8 月 18 日
移動済み: Image Analyst
2023 年 8 月 19 日
Start with y = [] and in each step of the for-loop running from 1 to n, concatenate y and i*ones(1,i).
0 件のコメント
その他の回答 (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!