简体   繁体   中英

Matlab: Search Columns and Add Values

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

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM