フィルターのクリア

Faster method for polyfit along 3rd dimension of a large 3D matrix?

29 ビュー (過去 30 日間)
Justin
Justin 2012 年 8 月 9 日
コメント済み: Jan 2022 年 2 月 11 日
I am currently working with large data cubes in the form of an MxNxP matrix. The P-dimension represents the signal at each (m,n) pixel. I must obtain a Z-order polynomial fit(where Z varies depending on the situation) for each signal in the data cube. Currently, I utilize a "for" loop to obtain the signal at each pixel, obtain the polynomial coefficients, then calculate the fitted curve. The code I use is fundamentally similar to the following:
dataCube = rand(1000,1000,300);
x = rand(300,1);
sizeCube = size(dataCube);
polyCube = zeros(sizeCube);
for ii = 1:sizeCube(1);
for iii = 1:sizeCube(2);
signal = squeeze(dataCube(ii,iii,:));
a = polyfit(x,signal,z)
y = polyval(a,x);
polyCube(ii,iii,:) = y;
end
end
Because of the quantity of iterations in the for loop, this operation takes a considerable amount of time for each data cube. Is there a faster way to obtain the polynomial fitting, without having to resort to the iterative process I use here. Perhaps, something similar to the filter function where you can apply the filter to a specific dimension of a matrix, rather than extracting each signal?
filteredCube = filter(b,a,dataCube,[],3)
Thanks, Justin
  2 件のコメント
Walter Roberson
Walter Roberson 2012 年 8 月 9 日
Have you considered re-ordering your data, at least during the processing, so that the dimension you are fitting over is the first dimension? Access (and assignment) over the first dimension is faster.
Justin
Justin 2012 年 8 月 14 日
Walter,
I attempted reordering the matrix and performing the process as above, over the 1st dimension. There was an incremental improvement in speed, but not nearly the type of advance I was hoping I could achieve.
Check out the answer by Teja below. Its exactly what I was hoping for and works perfectly.
Thanks for the input! Justin

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

採用された回答

Teja Muppirala
Teja Muppirala 2012 年 8 月 10 日
This can be accomplished in a fraction of the time with some matrix operations.
dataCube = rand(100,100,300);
sizeCube = size(dataCube);
x = rand(300,1);
z = 3;
V = bsxfun(@power,x,0:z);
M = V*pinv(V);
polyCube = M*reshape(permute(dataCube,[3 1 2]),sizeCube(3),[]);
polyCube = reshape(polyCube,[sizeCube(3) sizeCube(1) sizeCube(2)]);
polyCube = permute(polyCube,[2 3 1]);
  4 件のコメント
Pavel Psota
Pavel Psota 2022 年 2 月 10 日
+1, cool, indeed! Could the fitting coefficients be obtained from your solution?
Jan
Jan 2022 年 2 月 11 日
A small improvement is to avoid the expensive power operation:
% Replace:
V = bsxfun(@power,x,0:z);
% by:
V = [ones(300, 1), cumprod(repmat(x, 1, z), 2)];

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

その他の回答 (1 件)

Martin Offterdinger
Martin Offterdinger 2019 年 4 月 9 日
Dear Teja,
I am having a similar problem- actually a simpler one even. I have the same array, but I always need to fit a first-order polynom (linear, z=1 in your code). Is it possible to get the coefficients of the linear fit (p1,p2) from your solution as well?
Thanks,
Martin
  1 件のコメント
Jan
Jan 2019 年 4 月 9 日
編集済み: Jan 2019 年 4 月 9 日
@Martin: Please do not attach a new question in the section for answer. Open a new thread instead and remove this pseudo-answer. Including a link to this thread is a good idea. Thanks.
What's wrong with setting z=1? Which array is "the same" and why do you need to determine the fit multiple times for the same array? (Please explain this in your new question...)

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

カテゴリ

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