convert each character of an array to int
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mireia Boneta Camí
le 24 Oct 2020
Commenté : Ameer Hamza
le 24 Oct 2020
I have a char array like this '10000111101010110101' and I want to convert each 0 and 1 to int and obtain [1 0 0 0 0 1 1 ...]. I'd like to do it without fors, is it possible?
0 commentaires
Réponse acceptée
Ameer Hamza
le 24 Oct 2020
Easy peasy
x = '10000111101010110101';
y = x - '0'
2 commentaires
Ameer Hamza
le 24 Oct 2020
If you look at the ASCII code for the character, you can see what is happening.
For example,
x = '10000111101010110101';
y = x + 0
Result
>> y
y =
Columns 1 through 17
49 48 48 48 48 49 49 49 49 48 49 48 49 48 49 49 48
Columns 18 through 20
49 48 49
So, if you subtract the ascii code for '0', you will get actual numbers.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!