accumarray » Matlab and Simulink Tutorials (2023)

Table of Contents
Syntax Description examples Videos

Create array with rollup

Syntax

A = Akkumulation (subs, val)
A = Accumulation (subs, val, sz)
A = accumarray(subs,val,sz,fun)
A = accumarray(subs,val,sz,fun,fillval)
A = accumarray(subs,val,sz,fun,fillval,sparse)
A = accumulation ({subs1, subs2, …}, val, …)

Description

accumarray groups elements of a data set and applies a function to each group. A = accumarray(subs,val) creates an array A by accumulating elements of the vector val using the elements of subs as indices. An element's position in subs determines which vals value it chooses for the accumulated vector; the value of an element in subs determines the position of the accumulated vector in the output.

A = accumarray(subs,val,sz) creates a matrix A of size sz, where sz is a vector of positive integers. If subs is non-empty with N>1 columns, then sz must have N elements, where all(sz >= max(subs,[],1)). If subs is a non-empty column vector, then sz must be [M 1] where M >= MAX(subs). Specify sz as [] for the default behavior.

A = accumarray(subs,val,sz,fun) applies the fun function to each subset of elements of val. The default accumulation function is sum. To specify a different fun function, use the @ symbol (e.g. @max). The fun function must accept a column vector and return a character, logical, or numeric scalar, or a scalar cell. The return value A has the same class as the fun return values. Specify funny as [] for the default behavior.

A = accumarray(subs,val,sz,fun,fillval) places the scalar value fillval on elements of A that are not referenced by any set of subs. For example, if subs is empty, then A is repmat(fillval,sz). fillval and the return values ​​from fun must belong to the same class. The default value of fillval is 0.

A = accumarray(subs,val,sz,fun,fillval,isparse) creates an array A that is sparse if the scalar input issparse is logical 1 (that is, true), or full if issparse is logical 0 (fake ). A is full by default. If issparse is true, fillval must be null or [], and val and the output of fun must be double.

A = accumarray({subs1, subs2, ...}, val, ...) returns multiple subvectors in a cell array. You can use any of the four optional inputs (sz, fun, fillval, or issparse) with this syntax.

(Video) MATLAB - Simulink Tutorial for Beginners | Udemy instructor, Dr. Ryan Ahmed

Note If the indices in subs are not ordered, the fun shouldn't depend on the order of the values ​​in its input data.

The function processes the input as follows:

1.

Find out how many unique indexes are in subs. Each unique index defines a container in the output array. The maximum index value in subs determines the size of the output array.
2.

Find out how many times each index is repeated.

This determines how many vals elements accumulate in each container in the output array.
3.

Create an output array. The output array has either the maximum size (subs) or the size sz.
4.

Stack the entries in vals into containers using the values ​​of the indices in subs and apply fun to the entries in each container.
5.

Fill in the values ​​in the output for locations not referenced by Subs. The default padding is null; Use fillval to set a different value.

(Video) Introduction to MATLAB for beginners | How to use MATLAB | MATLAB Tutorial for beginners | Mruduraj

Note that subs must contain positive integers. subs can also be a cell vector with one or more elements, where each element is a vector of positive integers. All vectors must have the same length. In this case, subs is treated as if the vectors form columns of an index matrix. val must be a character, logical, or numeric vector whose length is equal to the number of lines in subs. val can also be a scalar whose value is repeated for all rows of subs.

examples

example 1

Create a 5 by 1 vector and sum values ​​for 1-D repeating indices:

bravery = 101:105; subs = [1; 2; 4; 2; 4]subs = 1 2 4 2 4 A = accumarray(subs, val)A = 101% A(1) = val(1) = 101 206% A(2) = val(2)+val(4) = 102 +104 = 206 0% A(3) = 0 208% A(4) = Valor (3) + Valor (5) = 103 + 105 = 208

example 2

Create a 4 by 4 matrix and subtract the values ​​of the 2D repeating indices:

value = 101:106; subs = [1 2; 1 2; 3 1; 4 1; 4 4; 4 1];B = Accumulation(subs,val,[],@(x)sum(diff(x)))B = 0 -1 0 0 0 0 0 0 0 0 0 0 2 0 0 0

The order of the indices is important:

value = 101:106; subs = [1 2; 3 1; 1 2; 4 4; 4 1; 4 1];B1 = Accumulation(subs,val,[],@(x)sum(diff(x)))B1 = 0 -2 0 0 0 0 0 0 0 0 0 0 -1 0 0 0

Example 3

Create a 2 x 3 x 2 matrix and add values ​​for 3D repeating indices:

value = 101:105; subs = [1 1 1; 2 1 2; 2 3 2; 2 1 2; 2 3 2];A = Accumulation(subs,val)A(:,:,1) = 101 0 0 0 0 0A(:,:,2) = 0 0 0 206 0 208

example 4

(Video) The Complete MATLAB Course: Beginner to Advanced!

Create a 2 by 3 by 2 matrix and add the values ​​natively:

bravery = 101:105; subs = [1 1 1; 2 1 2; 2 3 2; 2 1 2; 2 3 2];A = accumarray(subs, int8(val), [], @(x) sum(x,'nativo')))A(:,:,1) = 101 0 0 0 0 0A(:, :,2) = 0 0 0 127 0 127class(A)response = int8

Example 5

Pass multiple subscript arguments to a cell array.

1.

Create a 12 element vector V:

V = 101:112;

2.

Create three 12-element vectors, one for each dimension of the resulting matrix A. Notice how the indices of these vectors determine which elements of V accumulate in A:

% Index 1 Index 6 => V(1)+V(6) => A(1,3,1) % | | subfiles = [1 3 3 2 3 1 2 2 3 3 1 2]; colsubs = [3 4 2 1 4 3 4 2 2 4 3 4]; subsubs = [1 1 2 2 1 1 2 1 1 1 2 2]; % | % Index 4 => V(4) => A(2,1,2) % % A(1,3,1) = V(1) + V(6) = 101 + 106 = 207 % A(2, 1,2) = V(4) = 104

3.

Call accumarray and pass in the vectors of indices in a cell array:

(Video) MATLAB Crash Course for Beginners

A = accumarray({rowsubs colsubs pagsubs}, V) A(:,:,1) = 0 0 207 0 % A(1,3,1) ist 207 0 108 0 0 0 109 0 317 A(:,:, 2) = 0 0 111 0 104 0 0 219 % A(2,1,2) ist

Example 6

Create an array with the max function and fill all empty elements in this array with NaN:

Wert = 101:105; subs = [1 1; 2 1; 2 3; 2 1; 2 3];A = acumulación(subs, val, [2 4], @max, NaN)A = 101 NaN NaN NaN 104 NaN 105 NaN

Example 7

Create a sparse matrix using the prod function:

Tapferkeit = 101:105; subs = [1 1; 2 1; 2 3; 2 1; 2 3];A = accumarray(subs, val, [2 4], @prod, 0, true)A = (1,1) 101 (2,1) 10608 (2,3) 10815

example 8

Count the number of tickets accumulated in each container:

value = 1; subs = [1 1; 2 1; 2 3; 2 1; 2 3];A = Accumulation(subs, val, [2 4])A = 1 0 0 0 2 0 2 0

example 9

Create a logical array showing which containers accumulate two or more values:

value = 101:105; subs = [1 1; twenty-one; 23; twenty-one; 2 3];A = Accumulation(subs, val, [2 4], @(x) length(x) > 1)A = 0 0 0 0 1 0 1 0

Example 10

(Video) Complete MATLAB Tutorial for Beginners

Group values ​​in a cell array:

value = 101:105; subs = [1 1; twenty-one; 23; twenty-one; 2 3];A = accumarray(subs, val, [2 4], @(x) {x})A = [ 101] [] [] [] [2x1 double] [] [2x1 double] []A{ 2} answer = 104 102

See also

complete, rare, sum

Videos

1. [MBD Tutorial] How to develop a Model using MATLAB/Simulink
(Embitel Technologies)
2. MATLAB tutorial - Fuzzy Logic
(eeprogrammer)
3. MATLAB Tutorial
(Derek Banas)
4. Matlab Tutorial - 48 - Working with Matrices and the Symbolic Math Toolbox
(Math and Science)
5. Matlab Tutorial | Matlab Tutorial for Beginners - 2021| Matlab GUI | Great Learning
(Great Learning)
6. Use Python in MATLAB (Numpy & Gekko)
(APMonitor.com)
Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated: 03/19/2023

Views: 5367

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.