Matlab:搜索列并添加值 [英] Matlab: Search Columns and Add Values

查看:111
本文介绍了Matlab:搜索列并添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一般的编码我还是比较陌生的,我正在制作一个热图,这很简单.但是我只能处理一段数据.

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.

例如,我有一个如下所示的5x3数组:

I have an array 5x3 like below for example:

[9,9,1; 1,2,6; 3,6,2; 3,2,6; 5,6,2]

[9,9,1; 1,2,6; 3,6,2; 3,2,6; 5,6,2]

我想遍历第2列和第3列,并为每对第2列和第3列对汇总第1列. 在这种情况下,将导致9对1,9对,4对2,6对和8对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.

这是简化版本,我的第2,3列的值将为1:20 谢谢您的帮助

This is a simplified version, my columns 2,3 will have values from 1:20 Thanks for your help

推荐答案

使用accumarray在2/3列的基础上累积第一列作为索引.

use accumarray to accumulate the first column base on the 2/3 column as index.

查看测试代码

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

这篇关于Matlab:搜索列并添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆