Convert logical values into numeric values

521 次查看(过去 30 天)
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
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;

请先登录,再进行评论。

类别

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