How do I vectorize this?

조회 수: 2 (최근 30일)
Walter
Walter 2019년 4월 29일
댓글: Walter 2019년 4월 29일
I'm not sure how to even ask this question without code... I am trying to create a 2d matrix of sums from grouping variables--I tried splitapply but that didnt seem to get me exactly what I want, which is a way to do what I show below:
n = 100;
X = randsample(1:9, n, true);
Y = randsample(15:20, n, true);
Z = rand(n, 1);
[ji, ~, jL] = grp2idx(X);
[di, ~, dL] = grp2idx(Y);
nJ = numel(jL);
nD = numel(dL);
Ts = zeros(nJ, nD);
for i=1:nJ
for j=1:nD
Ts(i,j) = sum(Z(ji==i&di==j));
end
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 4월 29일
accumarray looks appropriate
Walter
Walter 2019년 4월 29일
yup! That's it! thank you!

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Encryption / Cryptography에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by