How to create this monotonically increasing list of numbers?

10 ビュー (過去 30 日間)
Dominik Mattioli
Dominik Mattioli 2018 年 7 月 11 日
回答済み: Michiele Ogbagabir 2018 年 7 月 11 日
I want to use a vector V of length N
V = [1 1 2 1 2 3 1]
to create a monotonically increasing list L, where the values of L are as follows:
L = [1 2 3 3 4 5 5 6 6 6 7]
So the entries 1:N of V dictate how many times V(N) is listed sequentially in L,
i.e. the 1st entry in V is listed in L a total of V(1) times..., the nth entry of V is listed in L a total of V(n) times.

採用された回答

Paolo
Paolo 2018 年 7 月 11 日
編集済み: Paolo 2018 年 7 月 11 日
V = [1 1 2 1 2 3 1]
n = 1:numel(V);
L = repelem(n,V)

その他の回答 (1 件)

Michiele Ogbagabir
Michiele Ogbagabir 2018 年 7 月 11 日
Here is one way
V = [ 1 1 2 1 2 3 1];
L = [];
for i = 1:length(V)
L = [L repelem(i, V(i))];
end
L

カテゴリ

Help Center および File ExchangeClocks and Timers についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by