Convert logical values into numeric values

671 ビュー (過去 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
  1 件のコメント
Adesanya Taiwo
Adesanya Taiwo 2024 年 3 月 2 日
This worked

サインインしてコメントする。


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 Center および File ExchangeData Type Conversion についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by