Mirza Tanweer Beig
Mirza Tanweer Beig

Reputation: 31

I want to convert the char matrix into numbers in matlab

I want to convert the string into numbers for example x=[abacaaaabb] I want to assign values a=1 b=2 and c=-1 and store in new matrix x=[1 2 1 -1....}

Upvotes: 2

Views: 113

Answers (2)

Dan
Dan

Reputation: 45752

A simpler way (unless your mapping really does have to be arbitrary like in your example):

x=['abacaaaabb'];

num = x - 96

results in

num =

     1     2     1     3     1     1     1     1     2     2

Upvotes: 0

paddy
paddy

Reputation: 63471

You can create a mapping:

map = zeros(1,256);
map('abc') = [1, 2, -1];

Then you can just index it with your input:

x = 'abacaaaabb';
mx = map(x);

Upvotes: 7

Related Questions