Is there a lookup function in matlab?

Hello,
I am simulating a markov chain using the code below.
transC = [zeros(size(T,1),1), cumsum(T,2)]; %cumulative sum of rows, we will use this to decide on the next step.
n = 10000;
states = zeros(1,n); %storage of states
states(1) = 32; %start at state 1 (or whatever)
for ii = 2:n
%Generate a random number and figure out where it falls in the cumulative sum of that state's trasition matrix row
[~,states(ii)] = histc(rand,transC(states(ii-1),:));
end
It combines a transition probability matrix and a random number to return a sequence of states. e.g 1,2,4,2,5,4,3.....
However, the states are actually letter codes, e.g AA, AC, DC
I have a have a variable called 'convert' where I have a list of the states and their corresponding letter codes. For example
1 AA
2 AC
3 DC
. .
. .
How could I lookup the actual state names and return them in a new variable?
Thank you

回答 (2 件)

Image Analyst
Image Analyst 2013 年 1 月 6 日

0 投票

Presumably your variable is a cell array where the first column is the state number (kind of unnecessary actually) and the second column is the letter code. So to get the letter code from some variable stateNumber that contains an integer, you'd simply do:
letterCode = convert{stateNumber, 2}; % convert is your cell array variable.

4 件のコメント

John
John 2013 年 1 月 7 日
Hello Image Analyst,
Thanks for your reply.
I'm sorry, I don't quite follow, do you mean I do the following?
transC = [zeros(size(T,1),1), cumsum(T,2)]; %cumulative sum of rows, we will use this to decide on the next step.
n = 10000;
states = zeros(1,n); %storage of states
states(1) = 32; %start at state 1 (or whatever)
for ii = 2:n
%Generate a random number and figure out where it falls in the cumulative sum of that state's trasition matrix row
[~,states(ii)] = histc(rand,transC(states(ii-1),:));
end
letterCode = convert{state, 2}; % convert is your cell array variable.
Thank you
Walter Roberson
Walter Roberson 2013 年 1 月 7 日
convert = { 1 'AA'; 2 'AC'; 3 'DC'; ... }
letterCodes = convert(states, 2);
John
John 2013 年 1 月 7 日
編集済み: John 2013 年 1 月 7 日
Thank you, it works.
Is there documentation for this? I don't understand how it works, as silly as that sounds.
John
Walter Roberson
Walter Roberson 2013 年 1 月 7 日
This is just array indexing of a cell array.

サインインしてコメントする。

Walter Roberson
Walter Roberson 2013 年 1 月 7 日

0 投票

Also you could look at containers.map

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

タグが未入力です。

質問済み:

2013 年 1 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by