フィルターのクリア

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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by