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