sum function usage for string
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Xuande Zhang
il 14 Set 2020
Commentato: Star Strider
il 14 Set 2020
Hello all,
I was working on an entry-level question "Number of 1s in a binary string", my answer was converting the number to string and use strfind function '1'.
Saw a very clean answer , I don't quite understand this. If x is a string, what can we get x-'0'
y = sum(x-'0')
0 Commenti
Risposta accettata
Star Strider
il 14 Set 2020
Subtracting ASCII 0 from an ASCII string converts the string to numeric values.
There is some controversy as to that being ‘correct’, however it definitely does work! (It takes advantage of the fact that everything in a computer is stored as numeric representations.)
2 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!