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

2 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

Star Strider
Star Strider am 22 Aug. 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 Kommentare
Seba.V
Seba.V am 22 Aug. 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 am 22 Aug. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by