필터 지우기
필터 지우기

Using accumarry and changing size of output

조회 수: 1 (최근 30일)
susman
susman 2021년 2월 21일
댓글: susman 2021년 2월 22일
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일
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일
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개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by