주요 콘텐츠

categorical형 배열 결합하기

이 예제에서는 categorical형 배열을 결합하는 방법을 보여줍니다.

categorical형 배열 생성하기

A 교실의 학생 25명이 점심 시간에 선호하는 음료가 포함된 categorical형 배열을 생성합니다.

rng("default")
A = randi(3,[25,1]);
A = categorical(A,1:3,["milk" "water" "juice"])
A = 25×1 categorical
     juice 
     juice 
     milk 
     juice 
     water 
     milk 
     milk 
     water 
     juice 
     juice 
     milk 
     juice 
     juice 
     water 
     juice 
     milk 
     water 
     juice 
     juice 
     juice 
     water 
     milk 
     juice 
     juice 
     juice 

categorical형 배열을 요약합니다.

summary(A)
A: 25×1 categorical

     milk              6 
     water             5 
     juice            14 
     <undefined>       0 

B 교실의 학생 28명이 선호하는 음료가 포함된 또 다른 categorical형 배열을 생성합니다.

B = randi(3,[28,1]);
B = categorical(B,1:3,["milk" "water" "juice"])
B = 28×1 categorical
     juice 
     juice 
     water 
     water 
     milk 
     juice 
     milk 
     milk 
     milk 
     milk 
     juice 
     juice 
     milk 
     juice 
     milk 
     water 
     water 
     juice 
     juice 
     milk 
     water 
     water 
     water 
     juice 
     juice 
     milk 
     juice 
     water 

categorical형 배열을 요약합니다.

summary(B)
B: 28×1 categorical

     milk              9 
     water             8 
     juice            11 
     <undefined>       0 

Categorical형 배열 결합

A 교실과 B 교실의 데이터를 단일 categorical형 배열 Group1에 결합합니다.

Group1 = [A;B]
Group1 = 53×1 categorical
     juice 
     juice 
     milk 
     juice 
     water 
     milk 
     milk 
     water 
     juice 
     juice 
     milk 
     juice 
     juice 
     water 
     juice 
     milk 
     water 
     juice 
     juice 
     juice 
     water 
     milk 
     juice 
     juice 
     juice 
     juice 
     juice 
     water 
     water 
     milk 
      ⋮

categorical형 배열 Group1을 요약합니다.

summary(Group1)
Group1: 53×1 categorical

     milk             15 
     water            13 
     juice            25 
     <undefined>       0 

다른 범주를 갖는 categorical형 배열 생성

추가 음료 옵션으로 탄산음료가 제공된 50명 학생의 데이터를 포함하는 categorical형 배열 Group2를 생성합니다.

Group2 = randi(4,[50,1]);
Group2 = categorical(Group2,1:4,["juice" "milk" "soda" "water"])
Group2 = 50×1 categorical
     juice 
     juice 
     milk 
     water 
     milk 
     soda 
     juice 
     water 
     milk 
     soda 
     soda 
     water 
     water 
     soda 
     juice 
     juice 
     milk 
     water 
     milk 
     water 
     juice 
     water 
     milk 
     juice 
     milk 
     soda 
     milk 
     milk 
     water 
     soda 
      ⋮

categorical형 배열을 요약합니다.

summary(Group2)
Group2: 50×1 categorical

     juice            12 
     milk             14 
     soda             10 
     water            14 
     <undefined>       0 

다른 범주를 갖는 배열 결합

Group1Group2의 데이터를 결합합니다.

students = [Group1;Group2]
students = 103×1 categorical
     juice 
     juice 
     milk 
     juice 
     water 
     milk 
     milk 
     water 
     juice 
     juice 
     milk 
     juice 
     juice 
     water 
     juice 
     milk 
     water 
     juice 
     juice 
     juice 
     water 
     milk 
     juice 
     juice 
     juice 
     juice 
     juice 
     water 
     water 
     milk 
      ⋮

결과로 생성된 categorical형 배열을 요약합니다. 이 결합은 첫 번째 입력값의 범주 목록의 끝에 두 번째 입력값에만 있는 범주인 soda를 추가합니다(milk, water, juice, soda).

summary(students)
students: 103×1 categorical

     milk             29 
     water            27 
     juice            37 
     soda             10 
     <undefined>       0 

categorical형 배열에서 범주 순서를 변경하려면 reordercats 함수를 사용합니다.

students = reordercats(students,["juice" "milk" "water" "soda"]);

categories(students)
ans = 4×1 cell
    {'juice'}
    {'milk' }
    {'water'}
    {'soda' }

categorical형 배열의 합집합

Group1Group2의 고유한 응답을 찾으려면 union 함수를 사용합니다.

C = union(Group1,Group2)
C = 4×1 categorical
     milk 
     water 
     juice 
     soda 

unionGroup1Group2의 결합된 값을 반복하지 않고 반환합니다. 이 경우, C는 결합된 배열 students의 범주와 동일합니다.

이 예제에 나와 있는 모든 categorical형 배열은 비순서형입니다. 순서형 categorical형 배열을 결합하려면 해당 배열이 순서를 포함하여 동일한 범주 집합을 가져야 합니다.

참고 항목

| | | | | |

도움말 항목