필터 지우기
필터 지우기

how to randomly generate an alphabet within a given range ?

조회 수: 33 (최근 30일)
roshan varkey
roshan varkey 2013년 12월 2일
댓글: Ramesh Singh 2021년 6월 30일
i want to randomly generate an alphabet between a given range,for example a range of A-J,and i want matlab to pick a alphabet between this range...how do i do it? i am a beginner at matlab and would really appreciate ur help!ThankYou!

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 12월 2일
편집: Azzi Abdelmalek 2013년 12월 2일
s='A':'J'
str=s(randi(numel(s)))

추가 답변 (3개)

Wayne King
Wayne King 2013년 12월 2일
편집: Wayne King 2013년 12월 2일
Do you want capital or lower case letters? You can just use the ascii representations
char(97:106)
or
char(65:74)
If you have the Statistics Toolbox you can randomly sample from those "populations"
idx = randsample(65:74,5); %choose five letters at random
char(idx)

Arjun P Kumar
Arjun P Kumar 2020년 9월 25일
AB
AC
AD
AE
AF
let these be the options to randomly generate...how can i do that?
  댓글 수: 1
Sterling Baird
Sterling Baird 2020년 10월 21일
편집: Sterling Baird 2020년 10월 21일
I suggest asking as a new question and linking to this post, but I wouldn't be surprised if this question has been asked already somewhere.

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


Ramesh Singh
Ramesh Singh 2021년 6월 27일
편집: Ramesh Singh 2021년 6월 30일
it is the function that can generate random alphabet between the given alphabet.. one more thing you can also input the ascii code of alphabet
function ra=randa(a,b)
%a=any number or alphabet
%b=any number or alphabet
l=double(a);
k=double(b);
if a>=65&b<=90
ra=char(randi([l k],1,1));
elseif a>=95&b<=122
ra=char(randi([l k],1,1));
else
error('you are entering wrong number or alphabet');
end
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 6월 27일
Your ranges are off
char(60:90) %what you use
ans = '<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ'
('A':'Z')+0 %actual
ans = 1×26
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
Your use of > and < is confusing. Use >= and <=

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by