LoginSignup
0
1

More than 5 years have passed since last update.

Octaveで文字列を扱う時に知っておくこと

Posted at

はじめに

スパムメールの判定時など文字データがテキストで与えられる場合がある。Octaveでこのような文字データを処理するにあたり、知っておきたい文字列の扱い方、覚えておきたい関数についてまとめる。

関数の用法

wordを行列として定義する。

octave
C = {'Trump','Abe'}

C =
{
  [1,1] = Trump
  [1,2] = Abe
}

size(C)
ans =   1   2

行列要素の抽出の仕方2通り。

octave
C(1)
ans =
{
  [1,1] = Trump
}

C{1}
ans = Trump

文字行列がword単位でなくアルファベット単位になる場合。

octave
C = ['Trump', 'Abe'] or C = ['Trump' 'Abe']
C = TrumpAbe

size(C)
ans =
   1   8

C = ['Trump Abe']
C = Trump Abe

size(C)
ans =
   1   9

文字列を空白で区切りwordに分断したい時、strsplitが使える。

octave
str1=['Abe Trump Abe Abe']
str1 = Abe Trump Abe Abe
size(str1)
ans =
    1   17

strsplit(str1)
ans =
{
  [1,1] = Abe
  [1,2] = Trump
  [1,3] = Abe
  [1,4] = Abe
}

文字列が1行1列の行列になっているとstrsplitは使えない。

octave
str2={'Abe Trump Abe Abe'}
str2 =
{
  [1,1] = Abe Trump Abe Abe
}
size(str2)
ans =
   1   1

strsplit(str2)
error: strsplit: S and DEL must be string values

文字列s1とs2が同じであれば1を返し、異なれば0を返す。

octave
s1={'Trump'}
s2={'Abe'}

strcmp(s1, s2)

ans = 0
0
1
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
0
1