I am trying to write a function for an optimisation routine that requires the (total) summation of a matrix divided element by element by a second matrix, where the second matrix has a third dimension returning a vector the size of that third dimension.
i.e. something along the lines of a(1:n) = sum(sum(A./B(:,:,1:n))
which doesn't work. Any ideas?
Thanks

2 件のコメント

Adam
Adam 2014 年 10 月 13 日
Can you give a simple example of inputs A and B?
Dale
Dale 2014 年 10 月 14 日
編集済み: Dale 2014 年 10 月 14 日
Hi Adam
A is a m x m matrix, and B is a m x m x n matrix.
I am aware I could do this with a loop (below), was just seeing if it is possible with direct indexing...
for i = 1:n
a(i) = sum(sum(A./B(:,:,i)))
end

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

 採用された回答

Guillaume
Guillaume 2014 年 10 月 14 日

1 投票

Use bsxfun to perform your division
a = squeeze(sum(sum(bsxfun(@rdivide, A, B))));

1 件のコメント

Dale
Dale 2014 年 10 月 14 日
Perfect, thanks!

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

その他の回答 (2 件)

Adam
Adam 2014 年 10 月 14 日

1 投票

c = bsxfun( @rdivide, A, B );
a = squeeze( sum( sum( c ) ) );
should give the answer you want. Obviously you could do it in one line if you prefer.

1 件のコメント

Adam
Adam 2014 年 10 月 14 日
Ah, too late! :)

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

Roger Stafford
Roger Stafford 2014 年 10 月 14 日

1 投票

sum(A(:))./sum(sum(B,1),2)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2014 年 10 月 13 日

回答済み:

2014 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by