MATLAB How to Multiply Two Vectors in MATLAB with Specific Conditions?

  • Thread starter Thread starter loveinla
  • Start date Start date
  • Tags Tags
    Vectors
AI Thread Summary
To multiply two vectors x and y in Matlab according to the specified formula, where each element is computed as x_i * y_{i-1} - y_n, the following approach can be taken. First, ensure that the vectors are of the same length n by prompting the user for input at the beginning of the program. A recommended method involves shifting vector x to the right by inserting a zero at the beginning and end of y, allowing for element-wise multiplication. After performing the multiplication, the first element of the resulting vector can be dropped, and the last element of y (y_n) can be added to adjust the final output. While a for loop can be used for this calculation, leveraging Matlab's built-in vectorized operations is preferred for efficiency, especially with larger vectors.
loveinla
Messages
11
Reaction score
0
Hi--I have two vectors ##x=(x_1, x_2, ..., x_n)## and ##y=(y_1, y_2, ..., y_n)##.

Now I want them to be multiplied in the following way:
for each ##i=1,2,..,n##, I need ##x_i*y_{i-1}-y_n##.

Can anyone help me on how to code this in Matlab?

BTW, I also want to input the length of the two vectors ##n## at the very beginning of the program. What do I need to do in order ensure the lengths of vectors ##x## and ##y## are the ##n## I specified as an input?

Thanks in advance!
 
Physics news on Phys.org
hi loveinla:

I don't know Matlab, but you will need to specify what happens for the special case i=1.

Good luck.

Regards,
Buzz
 
If you take advantage of the Matlab element wise multiply then you could simply write

Z= X.*Y

But that's not exactly what you want so instead shift the X right by one element by inserting a 0 element at the front and a 0 element at the end of Y so that they are now same length then use the elementwise multiply.

From there you can adjust the Z vector by dropping the first element and then add in the ##Y_n## term using an elementwise add.

Alternatively you could use a for loop and doing the computations in one pass but that's against the spirit of Matlab with its built in iteration feature and it's slower especially for long vectors.
 
Here's an example that might be helpful:
Code:
>>  A=[1 3 5 7 9]

A =

     1     3     5     7     9

>> B=[2 4 6 8 10]

B =

     2     4     6     8    10

>> A.*B

ans =

     2    12    30    56    90

>> A(1:5).*B(1:5)

ans =

     2    12    30    56    90

>> A(1:4).*B(1:4)

ans =

     2    12    30    56

>> A(1:4).*B(2:5)

ans =

     4    18    40    70
 
  • Like
Likes jedishrfu
jedishrfu said:
If you take advantage of the Matlab element wise multiply then you could simply write

Z= X.*Y

But that's not exactly what you want so instead shift the X right by one element by inserting a 0 element at the front and a 0 element at the end of Y so that they are now same length then use the elementwise multiply.

From there you can adjust the Z vector by dropping the first element and then add in the ##Y_n## term using an elementwise add.

Alternatively you could use a for loop and doing the computations in one pass but that's against the spirit of Matlab with its built in iteration feature and it's slower especially for long vectors.
Thanks, it helps!
 

Similar threads

Replies
2
Views
2K
Replies
2
Views
1K
Replies
4
Views
1K
Replies
3
Views
2K
Replies
2
Views
3K
Replies
1
Views
2K
Back
Top