Main Content

categorical 配列の要素の比較

この例では、categorical 配列で関係演算を使用する方法を示します。

文字ベクトルの cell 配列からの categorical 配列の作成

2 行 4 列の文字ベクトルの cell 配列を作成します。

C = {'blue' 'red' 'green' 'blue';...
    'blue' 'green' 'green' 'blue'};

colors = categorical(C)
colors = 2x4 categorical
     blue      red        green      blue 
     blue      green      green      blue 

colors は 2 行 4 列の categorical 配列です。

categorical 配列のカテゴリを一覧表示します。

categories(colors)
ans = 3x1 cell
    {'blue' }
    {'green'}
    {'red'  }

要素が等しいかどうかの判定

関係演算子 eq (==) を使用して、colors の 1 番目の行と 2 番目の行を比較します。

colors(1,:) == colors(2,:)
ans = 1x4 logical array

   1   0   1   1

これらの行では 2 列目の値のみが異なります。

配列全体と文字ベクトルの比較

categorical 配列 colors 全体と文字ベクトル 'blue' を比較して、すべての値 blue の位置を見つけます。

colors == 'blue'
ans = 2x4 logical array

   1   0   0   1
   1   0   0   1

colors には blue という要素が 4 つ含まれており、配列の各端にあります。

順序 categorical 配列への変換

colors 内のカテゴリに数学的な順序を付けます。色スペクトル red < green < blue の順序を表すカテゴリの順序を指定します。

colors = categorical(colors,{'red','green' 'blue'},'Ordinal',true)
colors = 2x4 categorical
     blue      red        green      blue 
     blue      green      green      blue 

categorical 配列の要素は変更されません。

colors の離散カテゴリを一覧表示します。

categories(colors)
ans = 3x1 cell
    {'red'  }
    {'green'}
    {'blue' }

順序に基づく要素の比較

colors の 1 列目の要素が 2 列目の要素より大きいかどうかを判定します。

colors(:,1) > colors(:,2)
ans = 2x1 logical array

   1
   1

1 列目の値 blue は両方とも 2 列目の対応する値 red および green より大きいことがわかります。

colors の要素で 'blue' より小さいものをすべて見つけます。

colors < 'blue'
ans = 2x4 logical array

   0   1   1   0
   0   1   1   0

関数 lt (<) は、値 green および red のすべての位置を 1 で示します。

参考

|

関連する例

詳細