Creating a random string of special characters and operators

조회 수: 20 (최근 30일)
Yusuf Oguzhan Turgut
Yusuf Oguzhan Turgut 2018년 12월 29일
댓글: Image Analyst 2018년 12월 29일
Hello,
I'm new in Matlab. I want to generate a random string that created by only special characters and operators. It won't include number or letter. For example, The generated random string will be like: [\-:/ I cannot create it
How can I generate it ?

답변 (2개)

Image Analyst
Image Analyst 2018년 12월 29일
Here is one way, if you want to specify the operator characters in advance:
% Define acceptable operators in advance.
operators = {'+', '-', '/', '*'};
% Get 14 random indices from that list.
indexes = randi(length(operators), 1, 15)
% Get a new character array with those characters in it.
s = cell2mat(operators(indexes))
and you'll get
s = '*--++**-+-**/**'
Otherwise you can define an ASCII range of characters:
% Define acceptable operators in advance.
operators = 31:47;
% Get 35 random indices from that list.
indexes = randi(length(operators), 1, 35)
% Get a new character array with those characters in it.
s = char(operators(indexes))
and you'll get
s = '/#/$"*/) %!".,%-+)%% $(&)', &)")('
adapt as needed.
  댓글 수: 3
Jan
Jan 2018년 12월 29일
@Yusuf: See:
char(31:47)
This is the list of ASCII values of the characters treated as "operators".
Image Analyst
Image Analyst 2018년 12월 29일
Not all of them are MATLAB operators like *, +, /, and -. Some of them are "special characters" which is also what you said you wanted. Aren't all characters up to ASCII 48 or '0' except for 32 which is the space "special characters" and a subset of those are MATLAB mathematical operators. If you want to specify exactly what characters you want to use, then use the first way I showed.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2018년 12월 29일
cc = setdiff(' ':'~', ['a':'z','A':'Z']);
cc(randi(length(cc), 1, HowManyRandomCharacters))

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by