Effacer les filtres
Effacer les filtres

how to randomly generate an alphabet within a given range ?

33 vues (au cours des 30 derniers jours)
roshan varkey
roshan varkey le 2 Déc 2013
Commenté : Ramesh Singh le 30 Juin 2021
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!

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 2 Déc 2013
Modifié(e) : Azzi Abdelmalek le 2 Déc 2013
s='A':'J'
str=s(randi(numel(s)))

Plus de réponses (3)

Wayne King
Wayne King le 2 Déc 2013
Modifié(e) : Wayne King le 2 Déc 2013
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 le 25 Sep 2020
AB
AC
AD
AE
AF
let these be the options to randomly generate...how can i do that?
  1 commentaire
Sterling Baird
Sterling Baird le 21 Oct 2020
Modifié(e) : Sterling Baird le 21 Oct 2020
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.

Connectez-vous pour commenter.


Ramesh Singh
Ramesh Singh le 27 Juin 2021
Modifié(e) : Ramesh Singh le 30 Juin 2021
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 commentaires
Walter Roberson
Walter Roberson le 27 Juin 2021
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 <=
Ramesh Singh
Ramesh Singh le 30 Juin 2021

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical 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