I am using the accumarray by using the following command on the first and second column of struct array. The first part of the code works good.
but the last two commands "went_from_to_countAge" and "went_from_to_probAge", does not work. any idea?
sequence = [1 1 1 1 2 2 2 2 2 3;...
1 1 1 1 1 1 1 1 1 1;...
1 1 1 1 1 1 1 1 1 1;...
1 1 1 1 1 1 1 2 2 2;...
1 1 1 1 1 1 1 1 1 1;...
2 2 2 2 2 2 2 2 2 2; ...
2 2 2 2 2 2 2 3 3 3; ...
2 3 3 3 3 3 2 2 2 2; ...
2 2 2 2 2 2 4 4 4 4]
% first part of the code
k = 1:size(sequence)
Age (k) = 25 + k
States = unique([sequence(:,k); sequence(:,k+1)]); % Find the unique rows based on the data in the first two columns.
[TF, fromstateAge] = ismember(sequence(:,k), States); % logical arrays
[TF, tostateAge] = ismember(sequence(:,k+1), States);
% second part of the code
went_from_to_countAge(:,k) = accumarray( [fromstateAge(:), tostateAge(:)], 1, []) % frequency of each unique number
went_from_to_probAge(k)= went_from_to_countAge ./ sum(went_from_to_countAge); % pribability of each unique number

1 件のコメント

dpb
dpb 2021 年 2 月 21 日
Attach a .mat file with a sample of the struct and an example of what the desired output should be.
Almost impossible to visualize such complexity without having an example at hand.

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

 採用された回答

Walter Roberson
Walter Roberson 2021 年 2 月 21 日

0 投票

I am not clear if you are saying that you want different sizes to be returned? If so then you either need to use a cell array or else you have to pack the results into a larger array with padding.
If instead the point is that the size returned is sort of by chance but you want a fixed size, then change the place you used [] to give the required output size, such as
A = accumarray( [data(1).fromstate(:,2), data(1).tostate(:,3)], 1, [5 5]);

7 件のコメント

susman
susman 2021 年 2 月 22 日
sequence = [1 1 1 1 2 2 2 2 2 3;...
1 1 1 1 1 1 1 1 1 1;...
1 1 1 1 1 1 1 1 1 1;...
1 1 1 1 1 1 1 2 2 2;...
1 1 1 1 1 1 1 1 1 1;...
2 2 2 2 2 2 2 2 2 2; ...
2 2 2 2 2 2 2 3 3 3; ...
2 3 3 3 3 3 2 2 2 2; ...
2 2 2 2 2 2 4 4 4 4]
States = unique([sequence(:,1); sequence(:,2)]); % Find the unique rows of A based on the data in the first two columns.
[TF, fromstate] = ismember(sequence(:,1), States); % logical arrays
[TF, tostateAge] = ismember(sequence(:,2), States);
went_from_to_count_age_25 = accumarray( [fromstate(:), tostateAge(:)], 1, []); % frequency of transitions from one state to another
s = sum(went_from_to_count_age_25, 2);
went_from_to_prob_age_25 = went_from_to_count_age_25 ./ s;
For example, for the above code, I calculate "went_from_to_prob_age_25" using the column 1 and column 2 of the matrix "sequence".
How can I make a loop, such that I can do that for
"went_from_to_prob_age_26" using column 2 and 3
"went_from_to_prob_age_27" using column 3 and 4
.....
susman
susman 2021 年 2 月 22 日
I have also tried to adjust the above code like below but end up with errors in last two commands.
sequence = [1 1 1 1 2 2 2 2 2 3;...
1 1 1 1 1 1 1 1 1 1;...
1 1 1 1 1 1 1 1 1 1;...
1 1 1 1 1 1 1 2 2 2;...
1 1 1 1 1 1 1 1 1 1;...
2 2 2 2 2 2 2 2 2 2; ...
2 2 2 2 2 2 2 3 3 3; ...
2 3 3 3 3 3 2 2 2 2; ...
2 2 2 2 2 2 4 4 4 4]
% first part of the code
k = 1:size(sequence)
Age (k) = 25 + k
States = unique([sequence(:,k); sequence(:,k+1)]); % Find the unique rows based on the data in the first two columns.
[TF, fromstateAge] = ismember(sequence(:,k), States); % logical arrays
[TF, tostateAge] = ismember(sequence(:,k+1), States);
% this part gives error
went_from_to_countAge(:,k) = accumarray( [fromstateAge(:), tostateAge(:)], 1, []) % frequency of each unique number
went_from_to_probAge(k)= went_from_to_countAge ./ sum(went_from_to_countAge); % pribability of each unique number
Walter Roberson
Walter Roberson 2021 年 2 月 22 日
k = 1:size(sequence)
Remember that if you use size with one input then it outputs a row vector. When you use a non-scalar as a bound in a colon operation, the operator takes the first element. Therefore the size() would end up being the number of rows. But you use k as a column index.
susman
susman 2021 年 2 月 22 日
What is the best way to write the code in this case?
Walter Roberson
Walter Roberson 2021 年 2 月 22 日
k = 1:size(sequence,2)
susman
susman 2021 年 2 月 22 日
and this remains as it is?
went_from_to_countAge(:,k) = accumarray( [fromstateAge(:), tostateAge(:)], 1, []) % frequency of each unique number
went_from_to_probAge(k)= went_from_to_countAge ./ sum(went_from_to_countAge); % pribability of each unique number
Sorry for asking these stupid questions, as I already spend enough time to figure it out.
susman
susman 2021 年 2 月 22 日
Thanks alot, This worked for me
A{i} = accumarray( [data(1).fromstate(:,i), data(1).tostate(:,i)], 1, [5 5]);

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で MATLAB Report Generator についてさらに検索

質問済み:

2021 年 2 月 21 日

コメント済み:

2021 年 2 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by