Convert logical values into numeric values
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (3)
Jonatan Tidare
am 26 Mär. 2018
3 Stimmen
double(A) works
1 Kommentar
Adesanya Taiwo
am 2 Mär. 2024
This worked
chaman lal dewangan
am 13 Mär. 2018
Bearbeitet: chaman lal dewangan
am 13 Mär. 2018
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 Kommentare
James Tursa
am 13 Mär. 2018
Bearbeitet: James Tursa
am 13 Mär. 2018
This doesn't work either. Have you tried it?
??? Undefined function or method 'eq' for input arguments of type 'cell'.
Julotek
am 21 Mär. 2018
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
am 12 Mär. 2018
Bearbeitet: Jonathan Patao
am 13 Mär. 2018
Try this:
B = cell2mat(A).*1;
4 Kommentare
James Tursa
am 12 Mär. 2018
I just did. It doesn't work:
>> A = {1 0;0 1}
A =
[1] [0]
[0] [1]
>> B = A.*1;
Undefined operator '.*' for input arguments of type 'cell'.
Star Strider
am 12 Mär. 2018
@James —
Yours confirms my result:
A = {[1 0 1 1 0] == 1};
B = A.*1;
It throws the same error.
Jonathan Patao
am 13 Mär. 2018
you're right, my fault. you need to convert from cell first. try:
B = cell2mat(A).*1;
Star Strider
am 13 Mär. 2018
... duplicating my Answer!
Kategorien
Mehr zu Data Type Conversion finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!