フィルターのクリア

filling up an m X n X o size matrix without loops

1 回表示 (過去 30 日間)
Pal
Pal 2012 年 10 月 13 日
Hi,
I am looking for an efficient way to 'fill up' a three-dimensional (m by n by o size) matrix M with elements based on a simple rule.
I have a number of matrices of size m X n X 1. I want the matrix M to contain weighted sums of these m X n X 1 size matrices. The weighted sums are obtained by the general formula
M(:,:,i) = f1(A, theta(i)) + f2(B, theta(i)) + f3(C, theta(i)) + ...
where f1, f2, f3, etc... are arbitrary functions and A, B, C, etc... are the m X n X 1 size matrices. theta is a 1 X o size vector containing angles (the functions f1, f2... are trigonometrical functions). The functions f1, f2... apply elementwise operations on A, B,... using a single value of theta, thus their outputs are also m X n X 1.
I can fill the matrix M up by looping through i=1:o and inserting the appropriate level M(:,:,i). However I feel that there must be a vectorized solution that works much faster.
I'd appreciate if you could give me ideas where to look for this functionality.
Thanks a lot,
Paul
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 13 日
what is the difference between nwmw1 and nxm?
theta is a 1x0 size? what does that mean?
Pal
Pal 2012 年 10 月 13 日
Azzi,
There is no difference between nXmX1 and nXm.
theta is 1 by 'o', not 1 by zero.

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

採用された回答

Matt J
Matt J 2012 年 10 月 13 日
編集済み: Matt J 2012 年 10 月 13 日
I doubt that the loop over i is what's slowing you down. My bet is that it's the evaluation of f1(A,theta), f2(B,theta), etc.... However, one possibility to avoid the loop over i is as follows;
theta=reshape(theta,1,1,[]);
M = bsxfun(f1,A, theta) + bsxfun(f2,B, theta) + bsxfun(f2,C, theta)...
It still seems to me that you need a loop over f1,f2,f3, etc... but a vectorized solution to that will require knowledge of what those functions look like.
  3 件のコメント
Matt J
Matt J 2012 年 10 月 13 日
編集済み: Matt J 2012 年 10 月 14 日
That will also depend greatly on the number of terms f1...fj that you have. In any case, BSXFUN is not my preferred solution. Now that we know the form of your f_i(), the more appropriate thing to do is,
theta=theta(:).'; %make sure it's a row vector
M=[A(:),B(:),C(:),...]*[trig1(theta); trig2(theta); trig3(theta);...];
M=reshape(M,m,n,[]);
Pal
Pal 2012 年 10 月 14 日
That is about five times faster than the for loop. Brilliant!
Thank you very much.

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by