convert an array of numbers into letters; 1 =a, 2 =b, 3 = c etc.

조회 수: 96 (최근 30일)
David Mehr
David Mehr 2013년 4월 10일
편집: Jay 2025년 9월 26일
So i have created a function that returns a row vector of numbers, Example:
[3, 6, 12, 1, 1, 3].
I want to write a new function that turns these numbers into letters. And in a form that i can call call it for any size set of results using a rule such as
1 = A, 2 = B, 3 = C, 4 = D, etc.
I see there is a num2str function but i dont really understand how to implement this as a function that will perform the conversion on each element in the vector.
What would be the best way to do this?

채택된 답변

Matt Kindig
Matt Kindig 2013년 4월 10일
Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
numbers = [3,6,12,1,1,3]
letters = Alphabet(numbers)
Or using the ASCII codes:
letters = char(numbers + 64);
  댓글 수: 6
Image Analyst
Image Analyst 2021년 1월 30일
@Ivan Mich, give a specific example of what you're starting with (data plus the look up table), and your desired/expected output.
Walter Roberson
Walter Roberson 2025년 9월 25일
Alphabet = 'ABCDEF'
Alphabet = 'ABCDEF'
numbers = [3,6,12,1,1,3];
map(Alphabet) = numbers;
map('DEADBEAF')
ans = 1×8
1 1 3 1 6 1 3 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

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

추가 답변 (2개)

Image Analyst
Image Analyst 2013년 4월 10일
Try this:
asciiValues = [3, 6, 12, 1, 1, 3]
asciiChars = char(asciiValues+'A'-1)
In the command window:
asciiValues =
3 6 12 1 1 3
asciiChars =
CFLAAC
  댓글 수: 2
Image Analyst
Image Analyst 2013년 4월 10일
For lower case you can either do lower(asciiChars), or do this:
asciiChars = char(asciiValues+'a'-1)
David Mehr
David Mehr 2013년 4월 10일
thank you

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


Jay
Jay 2025년 9월 25일
You can create a simple MATLAB function to convert numbers to letters like this:
function letterStr = num2letterVec(numVec)
% Convert each number in the vector to a letter (1=A, 2=B, ..., 26=Z)
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
% Use modulo 26 to handle numbers > 26
letterStr = letters(mod(numVec-1, 26)+1);
end
This function works for any size vector, and numbers greater than 26 will wrap around automatically.
If you want to explore similar number-to-letter conversions online, you can check numberstoletters.com for inspiration and alternative mappings.
  댓글 수: 2
Walter Roberson
Walter Roberson 2025년 9월 25일
I would suggest a minor adjustment:
letterStr = letters(mod(numVec-1, length(letters))+1);
This avoids the "magic number", and makes it easier to adjust if letters changes.
Jay
Jay 2025년 9월 26일
편집: Jay 2025년 9월 26일
make sense, thank you for updating the code..
function letterStr = num2letterVec(numVec)
%NUM2LETTERVEC Convert a numeric vector to letters (1=A, 2=B, ..., 26=Z)
% Handles numbers > 26 by wrapping around using modulo.
% Non-integer values are rounded to the nearest integer.
% Define the letters
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
% Round numbers to nearest integer
numVec = round(numVec);
% Convert numbers to letters using modulo
letterStr = letters(mod(numVec - 1, length(letters)) + 1);
end

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by