accumarray

  1 Example 1
  2
  3
  4 Create a 5-by-1 vector and sum values for repeated 1-D subscripts:
  5 val = 101:105;
  6 subs = [1; 2; 4; 2; 4]
  7 subs =
  8      1
  9      2
 10      4
 11      2
 12      4
 13
 14 A = accumarray(subs, val)
 15 A =
 16    101       % A(1) = val(1) = 101
 17    206       % A(2) = val(2)+val(4) = 102+104 = 206
 18      0       % A(3) = 0
 19    208       % A(4) = val(3)+val(5) = 103+105 = 208
 20
 21
 22     0     0
 23      0     0     0     0
 24      0     0     0     0
 25      2     0     0     0
 26
 27
 28 The order of the subscripts matters:
 29 val = 101:106;
 30 subs=[1 2; 3 1; 1 2; 4 4; 4 1; 4 1];
 31 B1 = accumarray(subs,val,[],@(x)sum(diff(x)))
 32
 33 B1 =
 34
 35      0    -2     0     0
 36      0     0     0     0
 37      0     0     0     0
 38     -1     0     0     0
 39
 40
 41 Example 3
 42
 43
 44 Create a 2-by-3-by-2 array and sum values for repeated 3-D subscripts:
 45 val = 101:105;
 46 subs = [1 1 1; 2 1 2; 2 3 2; 2 1 2; 2 3 2];
 47
 48 A = accumarray(subs, val)
 49 A(:,:,1) =
 50    101     0     0
 51      0     0     0
 52 A(:,:,2) =
 53      0     0     0
 54    206     0   208
 55
 56 Example 4
 57
 58
 59 Create a 2-by-3-by-2 array, and sum values natively:
 60 val = 101:105;
 61 subs = [1 1 1; 2 1 2; 2 3 2; 2 1 2; 2 3 2];
 62
 63 A = accumarray(subs, int8(val), [], @(x) sum(x,‘native‘))
 64 A(:,:,1) =
 65   101    0    0
 66     0    0    0
 67 A(:,:,2) =
 68     0    0    0
 69   127    0  127
 70
 71 class(A)
 72 ans =
 73     int8
 74
 75 Example 5
 76
 77
 78 Pass multiple subscript arguments in a cell array.
 79
 80 Create a 12-element vector V:
 81 V = 101:112;
 82
 83
 84
 85 Create three 12-element vectors, one for each dimension of the resulting array A. Note how the indices of these vectors determine which elements of V are accumulated in A:
 86 %        index 1   index 6 => V(1)+V(6) => A(1,3,1)
 87 %          |         |
 88 rowsubs = [1 3 3 2 3 1 2 2 3 3 1 2];
 89 colsubs = [3 4 2 1 4 3 4 2 2 4 3 4];
 90 pagsubs = [1 1 2 2 1 1 2 1 1 1 2 2];
 91 %                |
 92 %              index 4 => V(4) => A(2,1,2)
 93 %
 94 % A(1,3,1) = V(1) + V(6) = 101 + 106 = 207
 95 % A(2,1,2) = V(4) = 104
 96
 97
 98 Call accumarray, passing the subscript vectors in a cell array:
 99 A = accumarray({rowsubs colsubs pagsubs}, V)
100 A(:,:,1) =
101      0     0   207     0        % A(1,3,1) is 207
102      0   108     0     0
103      0   109     0   317
104 A(:,:,2) =
105      0     0   111     0
106    104     0     0   219        % A(2,1,2) is 104
107      0   103     0     0
108
109 Example 6
110
111
112 Create an array with the max function, and fill all empty elements of that array with NaN:
113 val = 101:105;
114 subs = [1 1; 2 1; 2 3; 2 1; 2 3];
115
116 A = accumarray(subs, val, [2 4], @max, NaN)
117 A =
118    101   NaN   NaN   NaN
119    104   NaN   105   NaN
120
121 Example 7
122
123
124 Create a sparse matrix using the prod function:
125 val = 101:105;
126 subs = [1 1; 2 1; 2 3; 2 1; 2 3];
127
128 A = accumarray(subs, val, [2 4], @prod, 0, true)
129 A =
130    (1,1)            101
131    (2,1)          10608
132    (2,3)          10815
133
134
135 Example 8
136
137
138 Count the number of entries accumulated in each bin:
139 val = 1;
140 subs = [1 1; 2 1; 2 3; 2 1; 2 3];
141
142 A = accumarray(subs, val, [2 4])
143 A =
144      1     0     0     0
145      2     0     2     0
146
147 Example 9
148
149
150 Create a logical array that shows which bins will accumulate two or more values:
151 val = 101:105;
152 subs = [1 1; 2 1; 2 3; 2 1; 2 3];
153
154 A = accumarray(subs, val, [2 4], @(x) length(x) > 1)
155 A =
156      0     0     0     0
157      1     0     1     0
158
159 Example 10
160
161
162 Group values in a cell array:
163 val = 101:105;
164 subs = [1 1; 2 1; 2 3; 2 1; 2 3];
165
166 A = accumarray(subs, val, [2 4], @(x) {x})
167 A =
168     [       101]     []              []     []
169     [2x1 double]     []    [2x1 double]     []
170
171 A{2}
172 ans =
173    104
174    102

Examples

时间: 2024-10-10 17:06:20

accumarray的相关文章

function [eigf,eigv,dof]=laplaceeig(node,elem,problem)

1 function [eigf,eigv,dof]=laplaceeig(node,elem,problem) 2 % 0-boundary eigenvalue problem 3 % problem='0-boundary' 4 [bdNode,Dirichet,inNode]=findboundary(elem); 5 %找出边界节点.边界边的节点编号.内部节点 6 N=size(node,1); %节点个数 7 NT=size(elem,1); %单元个数 8 Nin=N-size(b

Matlab编程基础

平台:Win7 64 bit,Matlab R2014a(8.3) "Matlab"是"Matrix Laboratory" 的缩写,中文"矩阵实验室",是强大的数学工具.本文侧重于Matlab的编程语言侧面,讲述Matlab的基本语法,以及用Matlab语言进行程序设计.值得一提的是,Matlab从R2014a版本开始支持中文语言了! 1.基本概念 Matlab默认启动后界面: Matlab有关的文件后缀: File Extension Des

【Matlab编程】Matlab高效编程技巧

1.默认状态下.matlab显示精度是short型,而默认的计算精度是double型.而且显示精度与计算精度没有关系. 2. 一仅仅失明的猫的问题:注意方法! 3.给数组预分配空间是主要的高效编程准则之中的一个.假设不预先分配内存详细的动态扩充数组内存的实现方式例如以下: 假设预分配空间,则仅仅将新加入的值放入连续的内存中,而不会将整个数组又一次分配空间. 4.matlab也能够使用window命令,仅仅须要在系统命令前加.或者dos:比如! shutdown -s 会关闭电脑.这样能够想到.我

MATLAB cell 数组介绍

转载:http://www.matlabsky.com/forum.php?mod=viewthread&tid=21089 由于工作需要,经常要处理大量的实验数据.基本都是由仪器导出来,随着社会发展的进步,人们获取数据的量在不断增长,很多人都是,现在已经是海量数据时代了... 从cell使用说起在读取文件的时候,cell数组(各种翻译都有,元胞数组,单元数组...直接无视)是Matlab的宠儿,基本都会出现,长期使用发现频率比struct高了不少~无论是Import Data还是使用texts

图像的归一化互信息Normlized Mutual Information

紧接上文: 计算二维离散随机变量的联合概率分布 我们知道了上文提到的几种计算二维概率密度分布中, accumarray 方法是最快的. 那么就使用accumarray来求计算两幅相同大小图像的归一化互信息. 互信息的定义 离散变量的互信息定义为: 求联合分布和边缘分布会用到了上文的方法. 或者使用熵来定义: 其中, H是熵. 熵是测量信号或者图像中信息量大小的量. 常用定义式: 归一化互信息定义为: 所以, 不论是求互信息还是求归一化互信息, 都要把两个随机变量的联合分布和边缘分布求出来. 而边

Matlab中所有自定义的函数

Functions By Category | Alphabetical List Language Fundamentals Entering Commands ans Most recent answer clc Clear Command Window diary Save Command Window text to file format Set display format for output home Send cursor home iskeyword Determine wh

查看线程CPU利用率

*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } a { color: #4183C4; } a.absent { color: #cc0000; } a.anchor { display: block; padding-left: 30px; margin-left: -30px; cursor: pointer; position: absolute