Simple string and how to produce all possible combinations of numbers, letters and special characters
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
sai charan bandi
il 18 Giu 2015
Commentato: sai charan bandi
il 18 Giu 2015
Hi everyone, I am facing some problem in using strings.
I want to produce all possible combinations of numbers,letters and special charactes.
for eg. a12,b12,c12,d12... 1a2,1a3,1a4,..1a9..1a, and so on and I want to pass these strings to some function in iterative for loop
If try to use for loop the problem will be as follows:
x='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,/<>?";:\|}]''{[=+-_)(*&^%$#@!~`';
for i=1:length(x)
for j=1:length(x)
Data='x(i)x(j)'
Data1=sprintf('%X',Data);
.....
.....
end
end
If i use for loop using 'x(i)x(j)' it will be considered as a single string. So for every loop it will produce same hex value!!
If I remove '' and only give x(i)x(j) it doesnt count as any data type and matlab will give error.
I want to use this Data1 hex value in another function , like this I want to increase the loop further to produce all possible strings with 3 places and 4 places and so on....
Please help me
0 Commenti
Risposta accettata
Guillaume
il 18 Giu 2015
Modificato: Guillaume
il 18 Giu 2015
This is basic string concatenation, achieved with [] (or horzcat, or strcat):
Data = [x(i), x(j)]
Or you could use sprintf:
Data = sprintf('%c%c', x(i), x(j))
However, using a loop for generating all combinations is a performance killer, particularly when you have a function, nchoosek, specially designed for that:
x='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,/<>?";:\|}]''{[=+-_)(*&^%$#@!~`';
allcombs = nchoosek(x, 2)
allhexs = cellfun(@(c) sprintf('%X', c), num2cell(allcombs, 2), 'UniformOutput', false)
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!