I got this error message 'Subscripted assignment dimension mismatch'

x=rand(16,1301);
y=rand(16,4);
k=0:0.01:1;
I=eye(size(x'*x));
b=zeros(size(x'*x));
for j=1:length(k)
b(:,j)=(pinv(x'*x+k(j)*I)*x')*y;
end
It seems the size of b(:,j)= 1301 x 1 and the size for (pinv(x'*x+k(j)*I)*x')*y= 1301 x 4. How to write a code for output variable to get equal size? Please help.

 採用された回答

Star Strider
Star Strider 2015 年 6 月 8 日

0 投票

This runs:
x=rand(16,1301);
y=rand(16,4);
k=0:0.01:1;
I=eye(size(x'*x));
b=zeros(size(x,2), size(y,2), size(k,2));
for j=1:length(k)
b(:,:,j)=(pinv(x'*x+k(j)*I)*x')*y;
end
I only ran it through k=6 because it takes an excruciatingly long time. But it doesn’t throw the error. The solution was the correct preallocation of ‘b’.

2 件のコメント

Ahmad Zul Izzi Fauzi
Ahmad Zul Izzi Fauzi 2015 年 6 月 8 日
Many thanks Strider. Its worked.
Star Strider
Star Strider 2015 年 6 月 8 日
My pleasure.
I would save ‘b’ to a .mat file so you can load it when you need it and so you do not have to recalculate it each time.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by