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

1 次查看(过去 30 天)
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!

采纳的回答

Star Strider
Star Strider 2019-8-22
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 个评论
Seba.V
Seba.V 2019-8-22
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 2019-8-22
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by