Element-wise product in Simulink

I would like to perform an element-wise multiplication (Hadamard product) between 2 matrices in Simulink. The "multiply" block only allows for element-wise multiplication when the dimensions of both arguments are equal. For example, I would like to perform the following in Simulink (which works in MATLAB):
a = [1,2,3
4,5,6
7,8,9];
b = [1,2,3];
c = a.*b; % element-wise multiplication [3X3] x [1X3]
The only method I have found that works in Simulink is to copy each row of the b vector to produce 2 matrices that have the same dimensions:
a = [1,2,3
4,5,6
7,8,9];
b = [1,2,3
1,2,3
1,2,3];
c = a.*b;
This method is not ideal because my matrices are very large and I am trying to avoid duplicating the b vector to save memory.

6 comentarios

TAB
TAB el 22 de Ag. de 2018
For element-wise multiplication (Hadamard product) in Matlab using ".*", both matrix dimesions must be same.
How can it work in Matlab ?
Ryan Takatsuka
Ryan Takatsuka el 22 de Ag. de 2018
Editada: Ryan Takatsuka el 22 de Ag. de 2018
In MATLAB, one dimension must be the same, and the resulting matrix is built from the Hadamard product of each row/column. For example, this works in MATLAB, but not Simulink:
>>> a = [1,2,3
4,5,6
7,8,9];
>>> b = [1,2,3];
>>> c = a.*b
c =
1 4 9
4 10 18
7 16 27
I would like to do this in Simulink.
Aquatris
Aquatris el 23 de Ag. de 2018
Editada: Aquatris el 23 de Ag. de 2018
I do not think you can. Matlabs newer versions are smart about it and I think it actually converts that 1x3 matrix to 3x3 matrix internally.
Walter Roberson
Walter Roberson el 23 de Ag. de 2018
R2016b and later implemented automatic expansion as if you had written bsxfun()
So what would be the most efficient way to perform this calculation? It seems like expanding the vector to a matrix internally is inefficient if the vector is very long.
It seems like doing the multiplication manually is the most efficient because the vector doesn't have to be expanded (I was hoping for a cleaner way to do this though since I am trying to accomplish this with pure Simulink blocks and no MATLAB functions):
c = zeros(3);
for i=1:size(a,1)
c(i,:) = a(i,:) .* b;
end
Walter Roberson
Walter Roberson el 23 de Ag. de 2018
I wonder if you could get somewhere switching between sample based and frame based ?

Iniciar sesión para comentar.

Respuestas (1)

madhan ravi
madhan ravi el 27 de Dic. de 2023

0 votos

Desired can be achieved using For Each or For Iterator subsystem

Categorías

Más información sobre Simulink en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 22 de Ag. de 2018

Respondida:

el 27 de Dic. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by