Reputation: 125
I am rather new to coding in general and I'm working on making a heat map, which is straightforward enough. But I am stuck in processing a piece of data.
I have an array 5x3 like below for example:
[9,9,1; 1,2,6; 3,6,2; 3,2,6; 5,6,2]
I want to scan through columns 2 and 3 and sum up column 1 when for each column 2&3 pair. In this case would result in 9 for 9,1 pair, 4 for 2,6 pair and 8 for 6,2 pair.
This is a simplified version, my columns 2,3 will have values from 1:20 Thanks for your help
Upvotes: 1
Views: 40
Reputation: 1544
use accumarray
to accumulate the first column base on the 2/3 column as index.
see test code
len=10;
maxidx=20;
data=[randi(100,len,1), randi(maxidx,len,1), randi(maxidx,len,1)];
output=accumarray(data(:,2:3),data(:,1), [maxidx, maxidx]);
Upvotes: 1