categorical 配列の要素の比較
この例では、categorical 配列で関係演算を使用する方法を示します。
categorical 配列の作成
categorical 配列を作成します。
C = ["blue" "red" "green" "blue"; ... "blue" "green" "green" "blue"]; colors = categorical(C)
colors = 2×4 categorical
blue red green blue
blue green green blue
categorical 配列のカテゴリを一覧表示します。
categories(colors)
ans = 3×1 cell
{'blue' }
{'green'}
{'red' }
要素が等しいかどうかの判定
関係演算子 eq (==) を使用して、colors の 1 番目の行と 2 番目の行を比較します。
colors(1,:) == colors(2,:)
ans = 1×4 logical array
1 0 1 1
これらの行では 2 列目の値のみが異なります。
配列全体と string の比較
categorical 配列 colors 全体と string "blue" を比較して、すべての値 blue の位置を見つけます。
colors == "blue"ans = 2×4 logical array
1 0 0 1
1 0 0 1
colors には blue という要素が 4 つ含まれており、配列の各端にあります。
順序 categorical 配列への変換
colors 内のカテゴリに数学的な順序を付けます。色スペクトル red < green < blue の順序を表すカテゴリの順序を指定します。categorical 配列の要素は変更されません。
colors = categorical(colors,["red" "green" "blue"],Ordinal=true)
colors = 2×4 categorical
blue red green blue
blue green green blue
colors のカテゴリを一覧表示します。
categories(colors)
ans = 3×1 cell
{'red' }
{'green'}
{'blue' }
順序に基づく要素の比較
colors の 1 列目の要素が 2 列目の要素より大きいかどうかを判定します。
colors(:,1) > colors(:,2)
ans = 2×1 logical array
1
1
1 列目の値 blue は両方とも 2 列目の対応する値 red および green より大きいことがわかります。
colors の要素で blue より小さいものをすべて見つけます。
colors < "blue"ans = 2×4 logical array
0 1 1 0
0 1 1 0
関数 lt (<) は、値 green および red のすべての位置を 1 で示します。