I have a vector signal in Simulink that I want to transform so that each value is replaced by the average of that value and the subsequent value. My first idea was using a MATLAB function block with the following code:
function y = fcn(u)
y = (u+circshift(u,1))/2;
However, this somehow changes the vector to a matrix and leads to an error message:
I don't fully understand this behavior, but it is probably caused by reasons described in the answers to a previous question:
Are there other ways to perform this function using standard Simulink blocks?

3 Kommentare

Mathieu NOE
Mathieu NOE am 3 Apr. 2025
In other words you want to do a average using the current and past value so either you use a FIR filter , or simply use a delay / memory block to store the previous sample (so you are re-creating a 2 taps FIR filter)
Walter Roberson
Walter Roberson am 3 Apr. 2025
The first thing I would wonder is whether u is a row vector or column vector; the distinction would make a difference to the circshift() call.
Bruce
Bruce am 3 Apr. 2025
Bearbeitet: Bruce am 3 Apr. 2025
"In other words you want to do a average using the current and past value so either you use a FIR filter , or simply use a delay / memory block to store the previous sample (so you are re-creating a 2 taps FIR filter) "
Actually, I want want the function to rearrange the vector signal within each iteration.
"The first thing I would wonder is whether u is a row vector or column vector; the distinction would make a difference to the circshift() call."
Thanks, that helped. u is a row vector, and the MATLAB function block seems to convert it to a column vector. I tested the function previously in MATLAB, and there it had no effect on the shape of the input vector. However, I found that the solution to my problem in Simulink is simply to invert y in the function:
function y = fcn(u)
y = ((u+circshift(u,1))/2)';

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 3 Apr. 2025

0 Stimmen

It is not really a matrix. I suggest debugging the MATLAB Function first. Provide a Constant vector input, e.g. (1:5), observe the output. Is that what you are expecting?
The error was caused by something else. There is no input data thus no dimension info. Look at the last error message and specify the dimension of the input signal of that Gain block.

1 Kommentar

Bruce
Bruce am 3 Apr. 2025
Thanks, that helped. Using a simplified model, I found that the MATLAB function block converts the vector form from a row to a column vector. This happens regardless of the function it contains, for example, even if I simply set u = y. The solution is described above.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2024b

Tags

Gefragt:

am 3 Apr. 2025

Kommentiert:

am 3 Apr. 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by