matlab - summing matrix columns based on another matrix -
i have 2 matrices of same size.
first matrix: weights 800 x 1250 second matrix: country_code 800 x 1250
each column observation.
what sum each column in weights matrix based on country_code. example below might explain better
weight country_code 20 25 30 15 20 12 12 12 12 12 40 10 20 5 10 10 10 10 10 10 10 35 25 50 40 5 5 5 5 5 30 30 25 30 30 12 12 12 12 12 list of country codes result matrix 5 10 35 25 50 40 10 40 10 20 5 10 12 50 55 55 45 50
my code follows doesn't give me correct answers.
int_ccy - number of unique country codes ccy - vector of unique country codes t = 1 : int_ccy wgts(t, :) = nansum(weight(country_code==ccy(t, 1), :)); end
the following should solve problem, although there's nicer way using logical indexing (i think) can't recall off top of head:
for t = 1 : int_ccy; wgts(t,:) = sum(weight .* (country_code == ccy(t, 1)), 1); end
Comments
Post a Comment