Function to get a particular sequence of integers

2 ビュー (過去 30 日間)
Eduardo de la Rosa
Eduardo de la Rosa 2020 年 10 月 6 日
コメント済み: Ameer Hamza 2020 年 10 月 6 日
Hi everyone!
My question is really simple to understand visually with an example, but I can't reach a solution. Maybe someone could bring me some help.
Example:
n=3;
Input: [1, 2, 3, 4, 5, 6, 7, 8... and so on] %always integers >=1
Output needed: [1, 2, 3, 1, 2, 3, 1, 2... and so on]
NOTE 1: input vector is not neccessarily sorted, thus
Input: [4,3,6,1,2,7,5,8] %always integers >=1
Output needed: [1,3,3,1,2,1,2,2]
NOTE 2: I believe mod(x,n) function could be useful in this case, but I don't know how exactly.
Thanks in advance!

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 10 月 6 日
編集済み: Ameer Hamza 2020 年 10 月 6 日
Try this
x = [4,3,6,1,9,2,7,5,8];
y = mod(x-1, 3)+1;
Result
>> y
y =
1 3 3 1 3 2 1 2 2
  2 件のコメント
Eduardo de la Rosa
Eduardo de la Rosa 2020 年 10 月 6 日
Yeah, it works perfectly. Really appreciated!
Ameer Hamza
Ameer Hamza 2020 年 10 月 6 日
I am glad to be of help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by