現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
inner matrix dimensions must agree error in the function?
1 回表示 (過去 30 日間)
古いコメントを表示
function [f] = objfun(W,VCVMatx)
%function for varience of the portfolio with swtN stocks
f =W*VCVMatx*W';
this is my function I am trying to run in the main program. However, when run in the main program, it keeps giving me :
Error using * Inner matrix dimensions must agree.
W is a 10(N)X1 vector, while VCVMatx is an NXN vector?
Does anyone see anything wrong, considering I multiplied it by a transposed W'
I tried running fmincon, but in the main program it says the function matrix dimensions do not agree.
f = (W'*VCVMatx)*W;
i=1;
while i<=rows(ERGrid);
ExpectedReturn = ERInc(i);
[W] = fmincon('objfun',x0,swtOther_A,swtOther_b,swtOther_Aeq,swtOther_beq,swtMinWt1,swtMaxWt1,[],options);
ERGrid(2:swtN+1,i) = W;
i=i+1;
end;
W' is a 1X10 vector while VCVMatx is a 10X10 matrix and W is 10X1 vector
fmincon cannot run as a result.
回答 (1 件)
Geoff Hayes
2014 年 5 月 30 日
The first matrix product (in the equation) is the problem. Since W is Nx1 and VCVMatx is NxN, then W*VCVMtx will fail due to the incompatible dimensions of the two matrices. If you are just expecting a single value, then the equation should be re-written as
f = W'*VCVMtx*W;
29 件のコメント
Geoff Hayes
2014 年 5 月 30 日
Type the commands that Star Strider indicated just prior to evaluating f to ensure that the dimensions of the matrices are compatible.
Geoff Hayes
2014 年 5 月 30 日
編集済み: Geoff Hayes
2014 年 5 月 30 日
Try the following to get the number of rows and columns of VCVMatx
VCVMatx_size = size(VCVMatx)
Sameer
2014 年 5 月 30 日
i did that, and it says
Inner matrix dimensions must agree.
Error in objfun (line 8) f = (W'*VCVMatx)*W;
so it is basically giving me the same error as before.
Star Strider
2014 年 5 月 30 日
I’ve never seen the size function return ‘undefined’ as output.
Go back and see how you created VCVMtx. If the code creating it is not very long, post it or the relevant part of it.
Sameer
2014 年 5 月 30 日
swtN = cols(Data); VCVMatx = cov(Data)*252;
data is imported from a .csv file and has 10 (swtN) stocks.
these equation are in the main program
Geoff Hayes
2014 年 5 月 30 日
@Sameer - you said a couple of comments ago that W is 1x10 (which is different from the original statement of 10x1). If that is the case, and VCVMtx is still 10x10 (what did size(VCVMtx) return?), then your original line of code (not the one I suggested above) should be valid
f = W*VCV*W';
Sameer
2014 年 5 月 30 日
when i type that same equation in the main program ( f = W'*VCVMtx*W;) it seems to work.
but it does not work when it is in the function.
Geoff Hayes
2014 年 5 月 30 日
Strange that it doesn't work in the function. Could you attach the function (m file) to a comment, using the paper clip button?
Sameer
2014 年 5 月 30 日
yes it does turn out to be a scalar. I am supposed to minimize this for fmincon
Geoff Hayes
2014 年 5 月 30 日
編集済み: Geoff Hayes
2014 年 5 月 30 日
The code is
function [f] = objfun(W,VCVMatx)
%function for varience of the portfolio with swtN stocks
global VCVMatx;
W_size = size(W);
VCVMatx_size = size(VCVMatx);
f = (W'*VCVMatx)*W;
end
Why is there a global variable with the same name as the input parameter? If this global variable has not been set then it is probably empty and so the error is thrown. Please comment out the line
% global VXVMatx;
and try again so that the second input (to the function) is used.
Sameer
2014 年 5 月 30 日
i took out the globa variable in the function and now it is saying:
Error using objfun (line 7) Not enough input arguments.
Error in fmincon (line 564) initVals.f = feval(funfcn{3},X,varargin{:});
Error in FEHW7 (line 183) [W] = fmincon('objfun',x0,swtOther_A,swtOther_b,swtOther_Aeq,swtOther_beq,swtMinWt1,swtMaxWt1,[],options);
Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
Geoff Hayes
2014 年 5 月 30 日
Okay - I don't have the fmincon function, but reading the documentation at http://www.mathworks.com/help/optim/ug/fmincon.html indicates that the function handle passed into fmincon must be for a function that accepts a vector and returns a scalar. Is that why you have tried to get around the limitation of only one input being passed - by using a global variable?
If that is the case, then I suppose you have to put that global variable back and remove the second input to your objfun. Then, assuming that the input vector is 1x10 and that the global variable VCVMtx is 10x10, then your f should be changed to
f = (W*VCVMatx)*W';
and the global variable VCVMatx must be set prior to the evaluation of fmincon.
Geoff Hayes
2014 年 5 月 30 日
I think the problem that you were originally experiencing was the the global variable VCVMatx was not defined to be a 10x10 matrix, it was just empty. And so if you multiply a non-empty matrix ( W ) with an empty matrix ( VCVMatx ) the message Error using * Inner matrix dimensions must agree is thrown. So it didn't matter which combination of f we were using (transpose of W on left or right) it would still fail.
At some point, the global variable must have been set. And now, with the correct order of the matrix multiplications for f, it works as expected.
But you will have to remember to set the global variable VCVMatx...
Sameer
2014 年 5 月 30 日
would you happen to know how to place two linear equality constraints in one fmincon function? thanks.
Geoff Hayes
2014 年 5 月 30 日
No, unfortunately I have never used that function (nor have the toolbox for it).
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)