LoginSignup
2
0

More than 5 years have passed since last update.

matlab 繰り返しの添字に一行で変換する

Posted at

はじめに

初投稿、matlabの小ネタです。

添字を変換する

matlabの添字は1から数えます。
そのため、以下のようにmodを取って添字の繰り返しに変換しようとすると…
当然ですが、5列目の値は0になります。
添字としてはここは5にしたいので、あとで0→5に変換します。

>> A = [1:5; 6:10]

A =

     1     2     3     4     5
     6     7     8     9    10

>> mod(A, 5)

ans =

     1     2     3     4     0
     1     2     3     4     0

>> ans(ans==0)=5

ans =

     1     2     3     4     5
     1     2     3     4     5

一行にしたい!

このためにわざわざ二行も使いたくない!と思い、以下のようにしました。

>> mod(A-1,5)+1

ans =

     1     2     3     4     5
     1     2     3     4     5

スッキリ!
以上、おしまーい。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0