Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Torsten

Posts: 1,459
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
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"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 = (i-51)*w_mu
> term_2 = (i-50)*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.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.