Home > front end >  Generating letters using 'for' loop in Matlab
Generating letters using 'for' loop in Matlab

Time:05-23

I have to generate 80 random letters of the English alphabet (capital letters) and display them in a row of 10 letters per row using a 'for' loop. I get an error because the limit is 26. What do I change in the code?

alfabet = 'A' : 'Z'; 
for i = 1 : 80
temp = alfabet(i);  
swop = floor(rand *26    1); 
alfabet(i) = alfabet(swop); 
alfabet(swop) = temp;
end 
for i = 1 : 10 : 80
disp(alfabet(i : i   9))
end

CodePudding user response:

Actually it could be an one-liner:

disp((char('A' randi(26,8,10)-1)));

Surely you can split it with a usage of "for":

alfabet=char('A' randi(26,1,80)-1);
for i = 1 : 10 : 80
    disp(alfabet(i : i   9))
end

If for whatever reason you have to use a "for" for the random letters:

for i=1:80
    alphabet(i)=char('A' randi(26)-1);
end

Frankly speaking, I don't find this part attractive.

  • Related