How to subtract from a vector a three dimentional matrix

1 回表示 (過去 30 日間)
gsourop
gsourop 2016 年 12 月 12 日
コメント済み: Adam 2016 年 12 月 12 日
Hi everyone,
I have a 1x12 vector and a 3x2x12 matrix, say i x j x t. I want to subtract for each i and for each j the corresponding vector of t. I've tried the following, but it doesn't work.
for t=1:12
for j=1:2; %size(inputs,1);
for i=1:3 %10;
MSFE_i(i,:,j)=mean((y(1:12)'-forecasts_test(i,j,:)).^2);
end
end
end;
any ideas of what i am missing here? Thanks in advance.

採用された回答

Adam
Adam 2016 年 12 月 12 日
編集済み: Adam 2016 年 12 月 12 日
for example:
a = ones( 3, 2, 12 );
v = linspace( 0, 1, 12 );
res = reshape( bsxfun( @minus, reshape( a, [], 12 ), v ), [3, 2, 12] );
Obviously you can generalise the hard coded numbers and you don't need all the linspace stuff - I just used that to create a vector for me to subtract and then check the results quickly.
  2 件のコメント
gsourop
gsourop 2016 年 12 月 12 日
Thank you very much for the quick response. My question is that you have created a new matrix v = linspace( 0, 1, 12 ); , but in my case the respective matrix has already values, ie it is a fixed vector with returns. How can I resize it to look like v = linspace( 0, 1, 12 ); ?
Adam
Adam 2016 年 12 月 12 日
My v above is 1x12 which is the same as what you said your vector was so you just use your vector instead of my linspace-created vector.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by