Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.


Torsten
Posts:
1,627
Registered:
11/8/10


Re: Error message: In an assignment A(I) = B, the number of elements in B and I must be the same.
Posted:
Oct 14, 2013 2:28 AM


"GC" wrote in message <l3dv4a$5ek$1@newscl01ah.mathworks.com>... > Hi, > > I kept receiving this error message "In an assignment A(I) = B, the number of elements in B and I must be the same." relating to W(i) of my code attached below. > > Could someone advise how do I rectify this error? How does a column vector gets 'stored' in a 'for loop'? > > Thanks very much. > > =================== > > x1range = ('D7:D17'); > mean_ret = xlsread(filename, sheet, x1range); > %%11 by 1 matrix > > x1range = ('G7:Q17'); > covar_matrix = xlsread(filename, sheet, x1range); > %%11 by 11 matrix > > col_one = ones(size(mean_ret))'; > %% col_one is a 1 by 11 matrix > > w_mu = 1/(col_one*inv(covar_matrix)*mean_ret)*inv(covar_matrix)*mean_ret; > > w_gmv = 1/(col_one*inv(covar_matrix)*col_one')*inv(covar_matrix)*col_one'; > > for i = 1:2 > term_1 = (i51)*w_mu > term_2 = (i50)*w_gmv > W (i) = term_1 + term_2 > R(i) = W(i)'*mean_ret > Var(i) = W(i)'*covar_matrix*W(i) > end > > plot(R,Var)
term_1 and term_2 are vectors whereas W(i) is a scalar. So your assignment W(i) = term_1 + term_2 is inconsistent. Use W(:,i) = term_1 + term_2 instead.
Best wishes Torsten.


Date

Subject

Author

10/13/13


GC

10/14/13


Torsten


