I am estimating the probabilities over time.
for example, my data matrix is 6 by 5 (where 6 is the number of simulations and 5 is the number of time periods).
data = [2, 2 , 1, 1, 1, 1; 2, 2 , 1, 1, 1, 2; 2, 2 , 1, 2, 1, 2; 2, 2 , 1, 2, 1, 2; 2, 3 , 2, 2, 1, 2]
States = unique([data(:,1); data(:,2)]) % Find the unique rows of A based on the data in the first two columns.
[TFinitial, fromstateAge27] = ismember(sequence(:,3), States) % logical arrays
[TF25, tostateAge27] = ismember(sequence(:,4), States) % logical array
went_from_to_countAge27 = accumarray( [fromstateAge27(:), tostateAge27(:)], 1, []) % frequency of transitions from one state to another
went_from_to_probAge27 = went_from_to_countAge27 ./ sum(went_from_to_countAge27) % probability of transition from one state to another
Now I want to make a for loop that runs the above code for each column of matrix "data".
For the above code (column 1 to 2), my output is "went_from_to_probAge27 "
for column 2 to 3, my output will be "went_from_to_probAge28"
from column 3 to 4, my output will be "went_from_to_probAge29" and so on.

댓글 수: 3

Rik
Rik 2021년 2월 16일
Why do you want numbered variables? In general that is not a good idea.
susman
susman 2021년 2월 16일
As I am developing a discrete time markov chain, in which transition probabilities are changiny every period. So I found it as the easiest way to to have seperate variable for each transition matrix for period t. Is there any better way of doing it?
Rik
Rik 2021년 2월 16일
Yes, by using the method Jan shows below. That way you can still use indexing to access the data, instead of having to generate the variable name every time you want use the variable.

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

 채택된 답변

Jan
Jan 2021년 2월 16일

0 개 추천

Naming a variable "went_from_to_probAge27" hides important information in the name. There this information can be accessed by really awkward methods only. See TUTORIAL: why and how to avoid EVAL
Use arrays instead. Because here you need 27 as data, a struct array would be efficient:
Data(1).Age = 27;
[TFinitial, Data(1).fromstate] = ismember(sequence(:,3), States);
[TF25, Data(1).tostate] = ismember(sequence(:,4), States)
...
Does this help to solve your problem?

댓글 수: 5

susman
susman 2021년 2월 16일
편집: susman 2021년 2월 16일
I am sorry but I could not get your code. Can you please explain it? As i want to run the for loop on a
10000 by 41 mattrix with (more than 10000 simulations and 41 time periods).
"data" is just an example.
Creating variables like "fromstateAge27" is a shot in your knee, because you hide the information 27 in the name of the variable. There it is really hard to access it later on.
Use arrays instead. Then it is easy to include the processing in a loop.
went_from_to_probAge27 = ...
went_from_to_probAge28 = ...
cannot be expanded by a loop, but here it is trivial:
went_from_to_prob(k) = ...
Age(k) = 27 + k;
Dear Jan, Thank yoy for the suggestions. I am now trying to follow what you have suggested, but ending up with errors. Can you please tell me, if I have written the code exactly as you suggested?
k = 1:40
Age (k) = 25 + k
States = unique([data(:,k); data(:,k+1)]); % Find the unique rows of A based on the data in the first two columns.
[TF, fromstateAge] = ismember(data(:,k), States); % logical arrays
[TF, tostateAge] = ismember(data(:,k+1), States);
went_from_to_countAge(k) = accumarray( [fromstateAge(k), tostateAge(k)], 1, [])
went_from_to_probAge(k)= went_from_to_countAge(k) ./ sum(went_from_to_countAge(k));
Jan
Jan 2021년 2월 22일
Yes, this looks fine.
susman
susman 2021년 2월 22일
yes, but only the last command gives an error. This one
went_from_to_countAge(k) = ...
it is because, the output matrix "went_from_to_countAge(k)" is changing its size for each k. and the code ends up with error.
I have also posted the question in a new link as:
any suggestion?

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2021년 2월 16일

댓글:

2021년 2월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by