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

조회 수: 2 (최근 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