Fill matrix with random letters and numbers

In MATLAB,I need to create a 36 by 20 matrix and then fill column by column, with a randomized selection from the characters A through Z and 0 through 9.
So each column will have the same characters but in a randomized order.
Your hints and suggestions are welcome.
Thank you.

 Accepted Answer

A9=['A':'Z' '0':'9';];
Data=zeros(length(A9),20);
for k=1:size(Data,2)
Data(:,k)=randperm(length(A9));
end
Data=A9(Data)

17 Comments

Thank you for your answer. I'm beginning to understand, though it may take some time. :(
Do you recommend any particular book(s)?
Reading the MATLAB document is a good start. Watch some video from Doug Hull form the Mathworks website help also. You can run the above code step by step, watch the value of the variable and understand the underline operation.
Thank you, Fangjun.
To help understanding, maybe try this simple one first:
a=['A':'D']
b=randperm(4)
c=a(b)
Another alternative way (little bit slower) but without the for loop:
A=['A':'Z' '0':'9'];
Data=cell2mat(arrayfun(@(x)randperm(length(A)),1:20,'uni',false)')';
Data=A(Data);
Thank you for your suggestions. Paul's method is advanced for me at this point.
How would I put a space between the letters of each outputed row? The reason is that when I import them into Excel, there is no delimiter.
Using this xlswrite('testexcel',Data) you don't need delimiter, each array value goes into its own cell
Thank you, Paulo!!
Paulo (or whoever else reads this)--
xlswrite('testexcel',Data) did indeed do what you said it would, but it created an excel workbook with two sheets--one with the numbers, the other with the text. How do you merge the two?
Thanks in advance.
Frank are you sure that it created two sheets?
What I see is one sheet with the letters and numbers, the letters appear on the left of each cell and the numbers on the right but this is the way excel seems to work, you can select all in excel and choose the text format for all cells, that way all appear on the left of the cells.
Paulo, what I see (pardon if I'm too explicit) is a file in the Current Folder that says testexcel.xls
When I click on that, the Import Wizard opens up; at the top there are two tabs--one labeled "data" on the left and one labeled "textdata" on the right. I click through the Import Wizard , select "Finish" and then the Import Wizard closes.
I know it's something simple, but what am I missing? Thanks for your patience.
Okay, you can one-right-click on the Excel file and choose "Open Outside MATLAB", it will open the file using Excel. You'll see the Excel file is right except numbers are at the left at each cell and letters are on the right of each cell. That is just the smart formatting of Excel.
What you saw was [Num,Text, Raw]=xlsread('ttestexcel.xls'). It tries automatically to separate numeric and text data. The original data is always in the variable 'Raw'.
Frank you didn't specify the use of the import wizard you just told me about the excel file, I also don't know why you are using the excel part, why don't you just save the data to a mat file?
Fangjun, thank you for your help.
I wanted to save the data to an excel file because it is easier for me to handle and to enter client data into.
Paulo, as I gain experience with MATLAB I'll use it more and more. I am less than a beginner with matlab--if there is a pre-neophyte category, then that describes me. Sorry for any confusion I caused.
No problem Frank, just in case you don't need the excel file you can just save the variable Data to a mat file
save('MyData','Data')
and load it when you need to use it
load('MyData')
the variable appears again on your workspace with the same name and contents.
Thank you again!!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!