How to turn a Yes/No cell array into binary (0-1)

I have a Yes/No cell array of 4000x1 and want to turn it into a binary system
Thanks for your attention

답변 (2개)

Jon
Jon 2022년 7월 5일
A = {true,false;false,true}
A = 2×2 cell array
{[1]} {[0]} {[0]} {[1]}
B = cell2mat(A)
B = 2×2 logical array
1 0 0 1

댓글 수: 5

It is a Yes No cell array but some of them display NA. At the moment I try to use cell2mat y mentions the following:
Error using cat
Dimensions of arrays being concatenated are not consistent.
Error in cell2mat (line 83)
m{n} = cat(1,c{:,n});
Error in BAR_CHART (line 19)
C_2 = cell2mat(B_2);
Thanks
Jon
Jon 2022년 7월 5일
Please save your array to a .mat file and attach it so that I can see exactly what you have
If your data are character arrays, with the words yes or no you can do this
A = {'Yes','no';'no','yes'}
A = 2×2 cell array
{'Yes'} {'no' } {'no' } {'yes'}
B = strcmpi(A,'yes') % use strcmpi rather than strcmp so it will be case insensitive
B = 2×2 logical array
1 0 0 1
When you say that your matrix contains NA, does that stand for Not Applicable. Since the result is binary true or false you can't have a binary array that has three kinds of values, so you have to pick either true or false for the NA's. The above code will work however it will consider NA to be false. If you want them to be true, you could do something like
B = strcmpi(A,'yes')|strcmpi(A,'NA')
Jon
Jon 2022년 7월 6일
Did this solve your problem? If so please accept the answer so others will know that a solution is available

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

ajay kumar
ajay kumar 2022년 7월 6일
m = {true,false;false,true};
for i = 1:numel(m)
disp(m(i))
end
{[1]} {[0]} {[0]} {[1]}

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2022년 7월 5일

답변:

2022년 7월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by