Error Using * and ^
Mostra commenti meno recenti
I am trying to ;
varV = sqrt(s^2*var('Texaco')^2 + (1-s)^2*var('Exxon')^2 + 2*s*(1-s)*cov(Texaco,Exxon))
but it gives me
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
After I arrange like this :
varV = sqrt((s.^2)*var(Texaco).^2 + ((1-s).^2)*var(Exxon).^2 + 2*s*(1-s)*cov(Texaco,Exxon))
It gives me :
Error using *
Inner matrix dimensions must agree.
How can I arrange this correctly ?
Note:Texaco and Exxon are my imported datas.
1 Commento
John BG
il 29 Feb 2016
have you considered using
cov(Texaco,Exxon)
Risposte (3)
Before you go any further, you must learn and understand the difference between dotted and non-dotted operators as per Stephen's link. Replacing operators at random until it produces a result is not a good way of working. You may get a result, but it may not be at all what you want.
For your equation to work (with either * or .*), s must have two columns. If you use *, the numbers of rows don't matter, if you use .* s needs to be a 2x2 matrix. Which operator you use depends on the operation you want to perform.
As per the documentation of cov, cov(a, b) is a 2x2 matrix. var(a) on the other hand is a scalar. So your equation is equivalent to:
y = s * scalar + s * scalar + s * 2x2 matrix
For s * 2x2 matrix to work s must have the same number of columns as the number of rows in the 2x2 matrix, so s must have 2 columns
If you were to change the * to .*, the equation would be equivalent to
y = s .* scalar + s .* scalar + s .* 2x2 matrix
s .* 2x2 matrix can only work if s is 2x2.
edit: For clarification your equation could be either:
varV = sqrt(s.^2*var(Texaco)^2 + (1-s).^2*var(Exxon)^2 + 2*(s.*(1-s))*cov(Texaco,Exxon))
or
varV = sqrt(s.^2*var(Texaco)^2 + (1-s).^2*var(Exxon)^2 + 2*(s.*(1-s)).*cov(Texaco,Exxon)) %note the difference in the last *
Both would work if s is 2 by 2 but would give different result. Only the former one would work if s is anything by 2.
varV = sqrt((s.^2).*var(Texaco).^2 + ((1-s).^2).*var(Exxon).^2 + 2.*s.*(1-s).*cov(Texaco,Exxon))
^ ^ ^ ^ ^
And learn about the difference:
5 Commenti
emine engin
il 29 Feb 2016
Modificato: emine engin
il 29 Feb 2016
emine engin
il 29 Feb 2016
I don't get any error using my code:
>> Exxon = [0.039473;-0.014466;0.086238;0.013513;-0.018644;0.074265;-0.009646;-0.01461;0.001647;-0.041118;0.010291;-0.039049;0.06007;0.036666;0.028938;0.059375;-0.016269;0.004566;-0.007575;0.088549;-0.021037;0.054441;-0.008152;0.013698;-0.029729;0.018105;-0.042407;0;-0.005714;0.020302;-0.008571;-0.020172;0.016176;0.004341;-0.007204;0.065312;-0.053133;0.063309;0.004059;-0.008086;-0.021739;-0.013888;0.011428;-0.009887;-0.014265;-0.024602;0.022255;0.020319;0.007112;0.038135;0.002721;0.002713;-0.012178;0;-0.004445;-0.043235;0.040816;-0.005602;-0.016901;0.014326;0.03531;0.005457;0.018995;-0.023968;0.047748;-0.005208;0.007853;0.040612;0.003797;0.008827;0.023749;-0.001221;0.020782;-0.007185;0.00965;-0.002389;0.005988;0.02619;-0.013921;0.021176;-0.014927;-0.023696;-0.002427;0.034063;0.014117;0.032482;0.016247;-0.045468;-0.013452;-0.018181;-0.018518;-0.028301;-0.015837;0.028423;0.007537;0.029925;-0.002421;0.014563;-0.011961;0.033898];
>> Texaco = [0;0.043478;0.078124;0.019512;-0.024154;0.049504;-0.028301;0.014563;-0.028708;-0.02463;0;-0.050505;0.021276;0.026041;-0.010152;-0.015812;0.05851;-0.015075;-0.010204;0.082474;-0.019047;0.03398;0.032863;-0.031518;-0.014084;0.071428;-0.048888;-0.028037;-0.019607;-0.015;-0.010152;-0.025641;0.005263;-0.005235;-0.015789;0.026737;-0.026041;0.016042;0;0.052631;-0.045;0.010695;-0.005291;-0.021276;0.038043;-0.010471;-0.026455;0.005434;0.016216;0.063829;0.02;0.004901;0.039024;-0.000265;-0.014354;-0.029126;0.02;-0.019607;0.005;0.004975;0.014851;0.039024;-0.004694;-0.037735;0.039215;0.028301;0.013761;0.004608;-0.027522;-0.014151;0.014354;0.023584;0.004608;0.004587;0.009132;0.009049;-0.013452;0.004545;-0.000165;0.013824;-0.00909;-0.018348;-0.004672;0.009389;0.013953;0.027522;0.017857;-0.021929;-0.040358;-0.004672;0.004694;0.03271;-0.045758;-0.009661;0.014634;-0.004807;0.028985;0.018779;0.009216;0.004566];
>> s = 1;
>> varV = sqrt((s.^2).*var(Texaco).^2 + ((1-s).^2).*var(Exxon).^2 + 2.*s.*(1-s).*cov(Texaco,Exxon))
varV = 7.5874e-004
John D'Errico
il 29 Feb 2016
Which always means that one or more of the variables supplied is not as the OP thinks it is, or that they have not used the code you wrote, probably missing a dot somewhere.
John D'Errico
il 29 Feb 2016
0 voti
Hmm. You used .^ there as directed. Perhaps you need to consider that for elementwise multiplies, you need to use .* also.
Note that scalar multiplies are expanded. So 2*x works, when x is a vector or array. But x*y will fail when both are vectors or arrays, IF your intention is elementwise multiplication. x*y is defined as a dot product.
Honestly, I wish that MATLAB had long ago used * for elementwise operations, and .* to designate a dot product. But my distorted sense of logic is not always accurate. It would be easier for novices to learn that way though.
Finally, you will also need to learn about the ./ operator too at some point. If not, you will be asking a similar question down the line. Thankfully, + and - do not need dots.
Categorie
Scopri di più su Multidimensional Arrays in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!