Indexing n-th element with increasing offset.

Good day
I'm trying to find a whey how it would be possible to index a n-th element with increasing offset in a arrey of zeros (expectation - 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1…). I found a whey, but its something like from first grade. Maybe someone would share some ideas, or give a hint of how it would be possible to reduce repeating typing…
x=1:1:100;
g=cumsum(x)<100;
g_idx=find(g);
X=zeros(size(x));
X(g_idx(1))=1;
X(cumsum(g_idx(1:2)))=1;
X(cumsum(g_idx(1:3)))=1;
X(cumsum(g_idx(1:4)))=1;
X(cumsum(g_idx(1:5)))=1;
X(cumsum(g_idx(1:6)))=1;
X(cumsum(g_idx(1:7)))=1;
X(cumsum(g_idx(1:8)))=1;
X(cumsum(g_idx(1:9)))=1;
X(cumsum(g_idx(1:10)))=1;
X(cumsum(g_idx(1:11)))=1;
X(cumsum(g_idx(1:12)))=1;
X(cumsum(g_idx(1:13)))=1

2 件のコメント

Mario Malic
Mario Malic 2020 年 8 月 6 日
編集済み: Mario Malic 2020 年 8 月 6 日
Pascal's triangle, binomial theorem? You can check on Wiki, there's a formula for those numbers on it.https://wikimedia.org/api/rest_v1/media/math/render/svg/23050fcb53d6083d9e42043bebf2863fa9746043
Andrew
Andrew 2020 年 8 月 6 日
Thank You for Your guidance.

サインインしてコメントする。

 採用された回答

Fangjun Jiang
Fangjun Jiang 2020 年 8 月 6 日

0 投票

%%
a=1:13;
b=cumsum(a);
X=zeros(1,100);
X(b)=1

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2020 年 8 月 6 日

コメント済み:

2020 年 8 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by