필터 지우기
필터 지우기

Converting a coloumn of characters into number for logistic regression

조회 수: 1 (최근 30일)
Ron Herman
Ron Herman 2020년 5월 1일
댓글: Stephen23 2020년 5월 1일
I have a coloumn that has Pass or Fail.
I want to assign Pass as 1 and Fail as zero for enntire coloumn.
a=['pass';'fail'; 'pass';'fail';'fail';'pass']
a =
6×4 char array
'pass'
'fail'
'pass'
'fail'
'fail'
'pass'
%Desired output
6×4 char array
1
0
1
0
0
1
% is this code correct???
for i=1:size(a,1)
if a(i)=='pass'
a(i)=1
else
a(i)=0
end

답변 (1개)

Stephen23
Stephen23 2020년 5월 1일
편집: Stephen23 2020년 5월 1일
For that character array:
>> v = all(a=='pass',2)
v =
1
0
1
0
0
1
If you really have a cell array of characte vectors use strcmp or strcmpi:
>> c = cellstr(a);
>> v = strcmpi(c,'pass')
v =
1
0
1
0
0
1
  댓글 수: 2
Ron Herman
Ron Herman 2020년 5월 1일
Sir I observed that the code is saving it as logical array.
Any line of code to convert it to double or integer type.

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

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by