Create a new matrix from a mapping process

4 Ansichten (letzte 30 Tage)
matthew arnott
matthew arnott am 18 Apr. 2013
I have mapped a set of numbers to a list of strings for example
KS = {'A','B','C'}; VS = [1:3]; x = containers.Map(KS,VS);
I have a 1 coloumn vector that looks like this
'A' 'B' 'B' 'C' 'A'
I want to create a new matrix which maps the letters to their respective numbers - so this would return 1 2 2 3 1
many thanks

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 18 Apr. 2013
Bearbeitet: Andrei Bobrov am 18 Apr. 2013
Vin = {'A' 'B' 'B' 'C' 'A'};
return1 = [Vin{:}] - 'A' + 1;
or
KS = {'A','B','C'};
VS = [1:3];
Vin = {'A' 'B' 'B' 'C' 'A'};
[~,ii] = ismember(Vin,KS);
out = VS(ii);
  1 Kommentar
matthew arnott
matthew arnott am 19 Apr. 2013
thanks for the help, however the fact that A = 1, B =2 etc is totally random it was just used as an example - however I have managed to loop through the matrix and vertcat a new matrix from the results of the containers.map function.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interpolation of 2-D Selections in 3-D Grids finden Sie in Help 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