
Math Software Math Software  Mathematica, Matlab, Calculators, Graphing Software 
 LinkBack  Thread Tools  Display Modes 
March 25th, 2017, 11:14 AM  #1 
Newbie Joined: Feb 2012 Posts: 18 Thanks: 0  A Q on mulplying two vectors in Matlab
HiI have two vectors $\displaystyle x = (x_1, x_2, ..., x_n)$ and $\displaystyle y = (y_1, y_2, ..., y_n)$. Now I want them to be multiplied in the following way: for each $\displaystyle i=1,2,..,n$, I need $\displaystyle x_i * y_{i1}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 $\displaystyle x$ and $\displaystyle y$ are the "n" I specified as an input? Thanks in advance! 
March 25th, 2017, 03:01 PM  #2 
Senior Member Joined: Feb 2016 From: Australia Posts: 1,356 Thanks: 464 Math Focus: Yet to find out. 
You can assign n to be whatever you want, then in your for loop, loop from i = 1:n, and then compute your sums. This will ensure that their lengths are n.

March 25th, 2017, 06:46 PM  #3 
Newbie Joined: Feb 2012 Posts: 18 Thanks: 0 
Is there anyway to use Matrix, rather than loop? Thanks.

March 25th, 2017, 07:17 PM  #4 
Senior Member Joined: Feb 2016 From: Australia Posts: 1,356 Thanks: 464 Math Focus: Yet to find out. 
Hmm.. Not efficiently no? If you want to compute your function for every value of i, then the best way would be to store those values in a matrix. Something like, for i = 1:n $\qquad$storage_mat(i) = x(i) * y(i1)  y(i); end Where i is the index and your storage matrix would be something like, storage_mat(1:n) = [x(1)*y(0)  y(1), x(2)*y(1)  y(2), ..., x(n)*y(n  1)  y(n)] Of course you will need to do some setting up before the for loop, and pay careful attention to your indexing in matlab (i.e., the first element in an array is 1, not zero). 

Tags 
matlab, mulplying, vectors 
Thread Tools  
Display Modes  

Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Using Matlab  MathAboveMeth  Math Software  1  December 21st, 2016 10:06 AM 
matlab  Elenashcher  Math Software  1  April 5th, 2015 07:03 AM 
Matlab help  WWRtelescoping  Math Software  1  March 3rd, 2015 12:07 PM 
matlab  Prakhar  Math Software  1  August 20th, 2014 01:47 AM 
Need Matlab Help  stealth4933  Math Software  1  April 30th, 2014 02:37 AM 