How to convert categorical array contains 'yes' or 'no' to logical array?

조회 수: 29 (최근 30일)
Hi, I have a table consist of categorical array which is just contains 'yes' and 'no', ex:
a = [yes yes no no yes no]
I want to convert it to a logical array translating yes to 1 and no to 0. ex:
b = [1 1 0 0 1 0]
Could you show me the way to do it? Thanks!
  댓글 수: 2
Jan
Jan 2018년 4월 16일
What is the type of your variable "a"? a = [yes yes no] is not valid Matlab code. Please post some code, which creates your input data. This is better than a rough description by words.
Bayu Ardiyanto
Bayu Ardiyanto 2018년 4월 16일
Hi sorry if it's not clear. However I think I've got it with answer from KSSV, many thanks! :)

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2018년 4월 16일

As stated in the documentation you can use the == operator to select elements in a categorical array that are in a particular category.

>> A = categorical({'yes','yes','no','no','yes','no'}) 
A = 
  1×6 categorical array
     yes      yes      no      no      yes      no 
>> A == 'yes'
ans =
  1×6 logical array
   1   1   0   0   1   0

추가 답변 (1개)

KSSV
KSSV 2018년 4월 16일
a = {'yes' 'yes' 'no' 'no' 'yes' 'no'} ;
b = strcmp(a,'yes')

카테고리

Help CenterFile Exchange에서 Categorical Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by