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
Best Answer