How to replace random integers with the alphabet (1=A, 2=B, and so on)

조회 수: 1 (최근 30일)
Jaime Vera
Jaime Vera 2018년 2월 5일
댓글: Jaime Vera 2018년 2월 5일
Hi, Thank you for helping me
I am trying to replace a square matrix of random integers with the alphabet
For instace: A = [1 2; 3 5] should give you A = [A B; C E]
My code is:
clear all; clc;
n = input('Choose your square matrix dimension: ')
N = round((27)*rand(n))
A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for kk = 1:length(N)*2
N(kk) = A(kk);
end
N
I do not know how to replace the numbers with the alphabet.
Could you help, please?
Thank you very much
Have a nice day.

채택된 답변

Guillaume
Guillaume 2018년 2월 5일
An easiest way to generate random integers is to use randi. As for your replacement it's achieved with simple indexing:
n = input('Choose your square matrix dimension: ');
N = randi(26, n);
A = 'A' : 'Z';
%replacement, note that this requires that all N are strictly positive integer:
NewN = A(N)

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 2월 5일
output = repmat(' ', size(N));
and then
output(kk) = A(N(kk));
However, notice that your N includes values that are 0: if the rand() produces a value less than 1/54 then multiplying by 27 would give less than 1/2 and round() of that would be 0.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by