how to multiply values between arrays columns to rows and generate a new vector form each of the multiplication?

2 visualizaciones (últimos 30 días)
given x= [ 1 1 1 ] and y=[ 1 2 3 ] how can I generate a code that will multiply each column value of x by each row of y and sote this values in a new array? How can I at the same time add a zero for every new array generated at the beginning (and end) of these vectors so that i can then add all of these vectors together?
ie:
x= [ 1 1 1 ] and y=[ 1 2 3 ]
x1=[1 2 3 0 0]
x2=[0 1 2 3 0]
x3=[0 0 1 2 3 ]
x4=x1+x2+x3
answer: x4=[1 3 6 5 3 ]
I hope my question is clear enough.
Thank you in advance!

Respuesta aceptada

Star Strider
Star Strider el 22 de Ag. de 2019
The operation you are looking for is not actually multiplication, it is convolution.
Example:
x = [ 1 1 1 ];
y = [ 1 2 3 ];
z = conv(x, y)
producing:
z =
1 3 6 5 3
as you describe yoiu want.
See the documentation on the conv function (and related links) for details.
  2 comentarios
Seba.V
Seba.V el 22 de Ag. de 2019
That's great thank you!
May I ask if it possible to add zeros to either side of an array (end or start) without overwrite the array values? I mean just expand the vector and be able to shift a set of values between the zeros?
like
x=[1 2 3]
x1=[ 0 1 2 3 0 0 0]
x2= [ 0 0 1 2 3 0 0]
x3=[ 0 0 0 1 2 3 0]
Star Strider
Star Strider el 22 de Ag. de 2019
My pleasure!
What you want to do is what the conv function does internally.
To do what you describe, try this:
x1 = zeros(1,6);
x2 = x1;
x3 = x1;
x1((1:3)+1) = y
x2((1:3)+2) = y
x3((1:3)+3) = y
producing:
x1 =
0 1 2 3 0 0
x2 =
0 0 1 2 3 0
x3 =
0 0 0 1 2 3
You can then add them:
z = x1 + x2 + x3
to get:
z =
0 1 3 6 5 3
Experiment to get the result you want.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by