Main Content

Sparse Matrix-Vector Product

Multiply sparse matrix with input vector

Since R2024b

  • Sparse Matrix-Vector Product block

Libraries:
HDL Coder / RCP and HIL

Description

The Sparse Matrix-Vector Product block multiplies the sparse matrix A with an input vector x.

Examples

expand all

This example builds a 2-dimensional matrix-vector multiplication function by using the Sparse Matrix-Vector Product block.

To implement this function, specify the parameter Matrix A as [1,2,0;3,0,4;0,5,6] and input vector x as [1,2,3] for the model.

load_system("SMVPblock2DExample")
sim("SMVPblock2DExample");
open_system("SMVPblock2DExample");

You can verify the results by performing 2-dimensional matrix-vector multiplication. In the MATLAB® Command Window, enter the value of matrix A and input vector x:

A = [1,2,0;3,0,4;0,5,6];
x = [1,2,3];

Compute x' and multiply with matrix A:

x = x';
y = A*x
y =

     5
    15
    28

This example builds a 3-dimensional matrix-vector multiplication function by using the Sparse Matrix-Vector Product block.

To implement this function, specify the parameter Matrix A as:

A(:,:,1) = [1,2,0;3,0,4;0,5,6]; A(:,:,2) = [1,2,3;3,5,4;7,5,6]; A(:,:,3) = [1,2,0;3,0,4;0,5,6];

and the input vector x as [1,2,3]. There is a Selector block to select the required matrix configuration based on specified indices. The Selector block has zero-based index mode where an index of 0 specifies the first element of the input vector. Therefore, for a 3-D input signal, 0, 1, and 2 are the valid input elements. In this example, the value of input for Sel is 2. For more information, see Selector.

load_system("SMVPblock3DExample")
sim("SMVPblock3DExample");
open_system("SMVPblock3DExample");

To perform 3-dimensional matrix-vector multiplication, in the MATLAB® Command Window, enter the value of matrix A and input vector x:

A(:,:,1) = [1,2,0;3,0,4;0,5,6];
A(:,:,2) = [1,2,3;3,5,4;7,5,6];
A(:,:,3) = [1,2,0;3,0,4;0,5,6];
x = [1,2,3];

For the 3-D input signal, the value of input signal Sel = 2 selects matrix A(:,:,3). Compute x' and multiply with matrix A(:,:,3):

x = x';
y = A(:,:,3)*x
y =

     5
    15
    28

Ports

Input

expand all

Input signal, specified as a scalar or vector. The Sparse Matrix-Vector Product block supports fixed-point data types. The number of input ports depends on the dimension of Matrix A. For example, see Model 3-D Matrix-Vector Multiplication.

Data Types: single | double | fixed point

Output

expand all

Output signal, returned as a scalar or vector. The output signal is the input signal multiplied by the sparse matrix specified by Matrix A.

Data Types: single | double | fixed point

Parameters

expand all

To edit block parameters interactively, use the Property Inspector. From the Simulink® Toolstrip, on the Simulation tab, in the Prepare gallery, select Property Inspector.

Main

Specify the sparse matrix to multiply the input vector by. Matrix A can be a real scalar, vector, or matrix.

Programmatic Use

To get the block parameter value programmatically, use the get_param function.

Parameter: A
Values: "1" | real scalar, vector, or matrix

Signal Attributes

Specify the minimum value of the output range that the software checks.

The software uses the output minimum to perform:

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: OutMin
Values: '[]' (default) | scalar in quotes

Specify the maximum value of the output range that the software checks.

The software uses the output maximum value to perform:

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: OutMax
Values: '[]' (default) | scalar in quotes

Choose the data type for the output. The type can be inherited, specified directly, or expressed as a data type object such as Simulink.NumericType.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: OutDataTypeStr
Values: 'Inherit: Inherit via internal rule' (default) | 'Inherit: Keep MSB' | 'Inherit: Match scaling' | 'Inherit: Inherit via back propagation' | 'Inherit: Same as first input' | 'double' | 'single' | 'half' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32' | 'int64' | 'uint64' | 'fixdt(1,16)' | 'fixdt(1,16,0)' | 'fixdt(1,16,2^0,0)' | '<data type expression>'

Parameter Attributes

Specify the minimum value of Matrix A. The default value is [] (unspecified). The software uses this value to perform:

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: ParamMin
Values: '[]' (default) | scalar in quotes

Specify the maximum value of Matrix A. The default value is [] (unspecified). The software uses this value to perform:

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: ParamMax
Values: '[]' (default) | scalar in quotes

Specify the data type of the Matrix A parameter.

While tuning a parameter with this Parameter data type setting, specify the Parameter minimum and Parameter maximum parameters. These settings tell the software about the range of values you want during the simulation and allows the software to provide a full precision data type with sufficient range to allow safe tuning within the specified range.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: ParamDataTypeStr
Values: 'Inherit: Inherit via back propagation' (default) | 'double' | 'single' | 'half' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32' | 'int64' | 'uint64' | 'boolean' | 'fixdt(1,16)' | 'fixdt(1,16,0)' | 'fixdt(1,16,2^0,0)' | 'Enum: <class name>' | '<data type expression>'

Extended Capabilities

Version History

Introduced in R2024b

See Also

|