Subtracting a vector from a variable-sized matrix in Simulink issue
47 views (last 30 days)
Show older comments
Hi everyone, I’d like to share a problem I’m facing in Simulink. I have a matrix with a variable number of rows (e.g., [m * 3]), where m is typically around 1000. I need to subtract a [1 * 3] vector from this matrix.
I cannot use the 'Subtract' block directly because of the dimension mismatch. I tried resizing the vector, but since the matrix row count is dynamic, I couldn't find a flexible solution. The vector's dimensions are fixed, but its values change over time, so the 'Bias' block isn't an option. Furthermore, I specifically want to solve this using standard Simulink blocks only, without using a 'MATLAB Function' block.
Has anyone dealt with this type of dynamic broadcasting in Simulink? Any advice on which blocks to use would be greatly appreciated.
1 Comment
dpb
less than a minute ago
Does seem like the perfect place to use a S-function, though, despite the wish. "If frogs had wings..." <g>
Answers (2)
Fangjun Jiang
about 19 hours ago
Edited: Fangjun Jiang
21 minutes ago
Would "m" vary during a simulation? If not and it only varies between simulations, then you can use the "For Each
Subsystem" block to construct a for-loop in Simulink. Not ideal, but dorable without using the "MATLAB Function" block, which BTW, is perfect to use for your use case.
Or, since it has only 3 columns, the below approach is practical.
Use the "Selector" block to pick out the column, use the "Matrix Concatenate" block to combine the columns.

0 Comments
Umar
on 16 Feb 2026 at 18:57
Edited: Walter Roberson
7 minutes ago
Hi @Kadir,
I looked into your problem and there's actually a perfect block for this, but it depends on whether you have the DSP System Toolbox installed.
If you have DSP System Toolbox, use the Array-Vector Subtract block. You'll find it under DSP System Toolbox > Math Functions > Matrices and Linear Algebra > Matrix Operations. This block is designed exactly for what you need - it subtracts a vector from a matrix along a specified dimension and officially supports variable-size signals, so your dynamic m value won't be a problem.
Setup is straightforward:
Connect your [m×3] matrix to port A
Connect your [1×3] vector to port V (set Vector source to "Input port")
Set "Subtract along dimension" to 1
The block will automatically broadcast your [1×3] vector across all m rows and subtract it from each row. Since the documentation confirms it supports variable-size signals, m can change during simulation without any issues.
If you don't have the DSP toolbox, you have a few workarounds using standard blocks, but they're all more complicated:
The simplest approach is to process each column separately. Use three Selector blocks to extract each column from your [m×3] matrix, then use three Subtract blocks to subtract each element of your [1×3] vector from the corresponding column, and finally use Matrix Concatenate to put the columns back together. It's not elegant, but it works and doesn't require loops.
Alternatively, there's a Reshape and Selector method that can replicate your vector to match the matrix size, but it's pretty involved and requires dynamically getting m using a Width block.
For Each Subsystem that Fangjun mentioned would work too, but looping 1000 times per timestep is going to be slow.
Honestly,if you don't have DSP System Toolbox and these workarounds seem too messy, this is exactly the kind of situation where a MATLAB Function block makes sense. I know you wanted to avoid it, but sometimes it's the right tool for the job. Just a few lines of code and you're done.
Hope this helps!
0 Comments
See Also
Categories
Find more on Array and Matrix Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!