Interpolating a 3D matrix

7 views (last 30 days)
Thomas Holmes
Thomas Holmes on 26 Mar 2019
Commented: Star Strider on 26 Mar 2019
I have a 14x14x221 matrix, A. Using a loop over the third dimension, I'm trying to interpolate the 14x14 matrix at each iteration of this loop to produce a new matrix that has more data points. Then save the new matrix at each iteration to form a new 3D matrix that has X,Y dimensions much greater than 14.
I have a script that does this for the rows but there is something wrong with the second nested for loop, which interpolates column-wise.
I keep getting this error "Unable to perform assignment because the size of the left side is 14-by-1 and the size of the right side is 1-by-221" for this line
NewestA(:,g,h) = R;
N = 221 ;
[m,n,p] = size(A) ;
NewA = zeros(m,N,p) ;
xi = linspace(1,n,N) ;
for i = 1:m % Splining row wise
for j = 1:p
T = interp1(1:n,A(i,:,j),xi) ;
NewA(i,:,j) = T ;
end
end
A = NewA ;
[m,n,p] = size(A) ;
NewestA = zeros(m,N,p) ;
yi = linspace(1,m,N);
for g = 1:n
for h =1:p
R=interp1(1:m,A(:,g,h),yi);
NewestA(:,g,h) = R;
end
end
A=NewestA

Answers (1)

Star Strider
Star Strider on 26 Mar 2019
I cannot follow what you are doing (or intend to do). However doing a 1D interpolation on a 3D matrix is likely not the correct approach. See if the interpn (link) or griddatan (link) functions will do what you want.
Note the grid-creating functions each prefers, specifically ndgrid (link) for interpn and meshgrid (link) for griddatan. They are not the same, and do not produce the same outputs.
  2 Comments
Thomas Holmes
Thomas Holmes on 26 Mar 2019
It is required for my assignment to use interp1 in this way. The first nested for loop produces NewA which is 14x221x221, instead of 14x14x221. It splines the data to refine the data.
The second nested loop should make NewestA which should be 221x221x221. But there is an issue with how i wrote the loop because the matrix dimensions do not agree in the line where R is set equal to NewestA(:,g,h)
Star Strider
Star Strider on 26 Mar 2019
I didn’t know this is homework.
My one suggestions is that with respect to the second loop, perhaps:
[m,n,p] = size(NewA) ;
might be more appropriate.

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!