필터 지우기
필터 지우기

Convert logical values into numeric values

조회 수: 514 (최근 30일)
Maria
Maria 2014년 8월 19일
댓글: Adesanya Taiwo 2024년 3월 2일
I have a cell array 20000 rows and 20 columns with logical values:
A={0 0 0 0 1 0...
0 0 1 0 0 0
1 0 0 0 0 0
0 1 0 0 0 0 ...}
And I would like to convert this array into numeric values of 1 and 0. Can someone help me? Thank you.

채택된 답변

Star Strider
Star Strider 2014년 8월 19일
This works:
B = double(cell2mat(A));
The cell2mat call converts it from a cell to a logical array, and double converts it to a double array.

추가 답변 (3개)

Jonatan Tidare
Jonatan Tidare 2018년 3월 26일
double(A) works

chaman lal dewangan
chaman lal dewangan 2018년 3월 13일
편집: chaman lal dewangan 2018년 3월 13일
I wrote small code
for a=1:number of rows
for b=1:number of columns
if A(a,b)==true
new_matrix(a,b)=1;
else
new_matrix(a,b)=0;
end
end
end
  댓글 수: 2
James Tursa
James Tursa 2018년 3월 13일
편집: James Tursa 2018년 3월 13일
This doesn't work either. Have you tried it?
??? Undefined function or method 'eq' for input arguments of type 'cell'.
Julotek
Julotek 2018년 3월 21일
for a=1:number of rows
for b=1:number of columns
if A{a,b}
new_matrix(a,b)=1;
else
new_matrix(a,b)=0;
end
end
end
That should work but it is maybe too complicated for you need.

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


Jonathan Patao
Jonathan Patao 2018년 3월 12일
편집: Jonathan Patao 2018년 3월 13일
Try this:
B = cell2mat(A).*1;
  댓글 수: 4
Jonathan Patao
Jonathan Patao 2018년 3월 13일
you're right, my fault. you need to convert from cell first. try:
B = cell2mat(A).*1;
Star Strider
Star Strider 2018년 3월 13일
... duplicating my Answer!

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by