Effacer les filtres
Effacer les filtres

reversing the digits into a string

1 vue (au cours des 30 derniers jours)
cgo
cgo le 10 Juil 2022
Commenté : John D'Errico le 10 Juil 2022
I have a list of numbers.
l1 = randi([0,9],[1,10]);
I want to make this into a string without the spaces and then reverse the digits.
Please help how to do this.

Réponses (1)

Kshittiz Bhardwaj
Kshittiz Bhardwaj le 10 Juil 2022
Hello cgo,
I understand that you want to convert your list into a string and then reverse it.
Follow these commands to get your desired result.
S = l1(:)';
This will convert your 2*2 array into a 1*4 array
str = char( S ) converts the input list to a char array
rev = reverse( str ) reverses the order of the characters in str .
  2 commentaires
cgo
cgo le 10 Juil 2022
sorry, didnt take away the spaces in between characters
John D'Errico
John D'Errico le 10 Juil 2022
Sorry, but this part is wrong:
S = l1(:)';
since that makes it into a coumn vector. And there is no reason to do so. l1 is a ROW VECTOR ALREADY. It is not a 2x2 array of numbers. You seem to have misunderstood what randi does.
l1 = randi([0,9],[1,10])
l1 = 1×10
5 6 0 6 3 3 9 7 9 1
Now, how do you convert those digits into characters? The use of char is also wrong, but subtly so. You need to convert them into their ascii representation as digits first, if you would use char. Adding '0' does that.
S = char(l1 + '0')
S = '5606339791'
Finally, you can use either flip or reverse to do the order swapping operation.
reverse(S)
ans = '1979336065'
flip(S)
ans = '1979336065'

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by