How to Multiply Two Vectors in MATLAB with Specific Conditions?

  • MATLAB
  • Thread starter loveinla
  • Start date
  • Tags
    Vectors
In summary, you can use the element wise multiply in Matlab to multiply two vectors, but to achieve the desired result for this specific problem, you will need to shift one of the vectors and adjust the resulting vector accordingly. Another option is to use a for loop, but this may be slower for longer vectors.
  • #1
loveinla
11
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
  • #2
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
 
  • #3
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.
 
  • #4
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
  • #5
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!
 

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

What is a vector?

A vector is a mathematical quantity that has both magnitude (size) and direction. It is often represented by an arrow, with the length of the arrow representing the magnitude and the direction of the arrow representing the direction.

How do you multiply two vectors?

The most common way to multiply two vectors is by using the dot product or scalar product. This involves multiplying the magnitudes of the two vectors and then multiplying them by the cosine of the angle between the two vectors. Another way to multiply vectors is by using the cross product or vector product, which results in a vector that is perpendicular to both of the original vectors.

What is the difference between multiplying two vectors and adding two vectors?

Multiplying two vectors results in a scalar quantity, while adding two vectors results in a vector quantity. This means that multiplying two vectors gives you a single number, while adding two vectors gives you a new vector with a magnitude and direction.

Why is it important to know how to multiply vectors?

Multiplying vectors is an important tool in mathematics and physics. It is used to calculate work and energy, find the angle between two vectors, and determine the direction of a force. It is also used in applications such as computer graphics and engineering.

Can any two vectors be multiplied together?

No, not every pair of vectors can be multiplied together. The dot product can only be performed on two vectors with the same number of dimensions, while the cross product can only be performed on three-dimensional vectors. Additionally, the order in which you multiply the vectors matters, as the dot product is commutative (A*B = B*A), but the cross product is not (AxB = -BxA).

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
962
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Linear and Abstract Algebra
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Linear and Abstract Algebra
Replies
13
Views
1K
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top