Main Content

categorical형 배열 요소 비교하기

이 예제에서는 categorical형 배열에 관계 연산을 사용하는 방법을 보여줍니다.

문자형 벡터로 구성된 셀형 배열에서 categorical형 배열 생성하기

문자형 벡터로 구성된 2×4 셀형 배열을 생성합니다.

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의 첫 번째 행과 두 번째 행을 비교합니다.

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

   1   0   1   1

두 행 간에는 두 번째 열의 값만 다릅니다.

전체 배열과 문자형 벡터 비교하기

전체 categorical형 배열 colors를 문자형 벡터 'blue'와 비교하여 blue 값이 있는 모든 위치를 찾습니다.

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

   1   0   0   1
   1   0   0   1

colors에는 배열의 코너마다 하나씩 총 4개의 blue 항목이 있습니다.

순서형 categorical형 배열로 변환

colors의 범주에 수학적 정렬(Mathematical Ordering)을 추가합니다. 색 스펙트럼의 순서를 나타내는 범주 순서 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의 첫 번째 열의 요소가 두 번째 열의 요소보다 큰지 확인합니다.

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

   1
   1

첫 번째 열의 두 값(blue)이 두 번째 열의 대응값(각각 redgreen)보다 큽니다.

colors에서 'blue'보다 작은 요소를 모두 찾습니다.

colors < 'blue'
ans = 2x4 logical array

   0   1   1   0
   0   1   1   0

함수 lt(<)는 모든 green 값과 red 값의 위치를 1로 나타냅니다.

참고 항목

|

관련 예제

세부 정보