how to simplify multi- if statement

I'm trying to do the following work, given a string of 'ABCDE' etc,
for each letter, it will be assigned with a unique number.
How would I do it without putting 26 if and else if statement in a simple way?
I'm thinking of two matrices. But it may not be on the right track. Can anyone help with this case? Thanx in advance!

1 Kommentar

Qian
Qian am 6 Dez. 2012
sorry for the bad description. I'm saying, the assignments between letters and numbers are given, what I want is to replace letter with numbers in the given way. The only method I was using is the if and else if statement with one-to-one assignment, but with longer string of letters, I don't really like to put 26 statement, is there any quicker way?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Matt Fig
Matt Fig am 6 Dez. 2012
Bearbeitet: Matt Fig am 6 Dez. 2012

0 Stimmen

STR = char(65:90)
KEY = randi(10000,1,length(STR))
But what are you going to do with these that you would need an IF block for? Are you going to do something like encode a given string?
g_str = upper('Hello')
k_str = KEY(g_str-64)

8 Kommentare

Qian
Qian am 6 Dez. 2012
thanx for that as well, but sorry for my bad description. I'm saying, the assignments between letters and numbers are given, what I want is to replace letter with numbers in the given way. The only method I was using is the if and else if statement with one-to-one assignment, but with longer string of letters, I don't really like to put 26 statement, is there any quicker way?
Matt Fig
Matt Fig am 6 Dez. 2012
That is what my second bit of code does..... Just imagine the KEY is the given set of numbers for all the letters in STR. Then to replace the letters in g_str, index into KEY like I did.
Qian
Qian am 6 Dez. 2012
Yep, I think I see what u meant now. Thanx!
Qian
Qian am 6 Dez. 2012
can I ask one more question, why is it -64?
Qian
Qian am 6 Dez. 2012
is it because u used char function?
Qian
Qian am 6 Dez. 2012
I think that's still not going to solve my problem fully, since My string is not in alpha beta order, and it's not fully 26 letters.
Matt Fig
Matt Fig am 6 Dez. 2012
But the same concept should apply. I subtracted 64 because we need to index starting from 1 and the ascii alphabet starts at 65.
Jos (10584)
Jos (10584) am 6 Dez. 2012
Using randi, it is not guaranteed that each character in STR is mapped onto a unique value ...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Rick Rosson
Rick Rosson am 6 Dez. 2012

0 Stimmen

aStr = 'ABCDE';
x = uint8(aStr) - 64;

1 Kommentar

Qian
Qian am 6 Dez. 2012
thanx for that, but the problem is ABCDE are not assigned to patterned numbers, but some random numbers, for instance A=14, B=23, C=1, D=4,E=34 etc.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by