Interpolation for n-dimensional array data

25 views (last 30 days)
Hello, I have this function in which I calculate Uvals,Svals and Vvals and store them in array. In this case I have 4D data. I have attached the .mat file.
rho_grid=[-1,0,1];
Nrho=length(rho_grid);
for i=Nrho:-1:1
rho1=rho_grid(i);
for j=Nrho:-1:1
rho2=rho_grid(j);
[Uvals(:,:,j,i),Svals(:,:,j,i),Vvals(:,:,j,i)]= svd(pro(rho1,rho2)); % two 'for' loops for 2 parameters 'rho1' and 'rho2' How to automate this for 'n' parameters?
end
end
I would like to retrieve entries of Uvals, Svals and Vvals like function of 2 variables 'rho1' and 'rho2'.
U = @(rho1,rho2) Uvals(...);
S= @(rho1,rho2) Svals(...);
V= @(rho1,rho2) Vvals(...);
Should I be using 'interp2' function for this? If have n-dimensional data, how can i make use of 'interpn' in my code? I had previously asked question in this context(https://de.mathworks.com/matlabcentral/answers/497657-accessing-function-output-for-different-values-of-variable?s_tid=prof_contriblnk), but I seek your help again. Thank you.

Answers (2)

Christine Tobler
Christine Tobler on 22 Jan 2020
I don't think interpn would work very well for you: The U, S and V matrices returned by SVD are not linearly dependent on the input matrix to SVD, so linearly interpolating U, S, and V based on some grid points will not give the U, S and V of the matrix for a given set of points rho1 and rho2.
I think the only reliable way to do this is to just call svd(pro(rho1, rho2)) inside of those function handles.
What's the background for wanting to compute the SVD on some grid of rho1 and rho2 ahead of time? Is this to reduce computation time in the three function handles? If you can give some information about how you're planning to use those function handles, that would be helpful. For example: Do you always call all three of them? Do you call them many times for values rho1 and rho2 that are on grid points? For scattered values of rho1 and rho2?
  1 Comment
Sandeep Parameshwara
Sandeep Parameshwara on 22 Jan 2020
Hello Christine, I think I can avoid this interpolation , thanks for your reply. It made me realize my code. I did this now:
for i=Nrho:-1:1
rho1=rho_grid(i);
for j=Nrho:-1:1
rho2=rho_grid(j);
[U,S,V]= svd(pro(rho1,rho2));
[Uvals(:,:,j,i),Svals(:,:,j,i),Vvals(:,:,j,i)]= svd(pro(rho1,rho2)); % I need this later for plotting
Ti= inv(sqrt(S))*U'*Rq(rho1,rho2);
T = Rp(rho1,rho2)*V*inv(sqrt(S));
systrans(:,:,j,i)= ss2ss(sys.Data(:,:,j,i),Ti);
end
end
I have some more queries in this. as you can see,
1) I have 2 'for' loops now to determine value of parameters 'rho1' and 'rho2'. In my case, this parameters will increase to very large number. If I have rho1,....,rho10 , I dont want to create all extra for loops manually. Do you see any way that I can automate it? Please note that rho_grid = [-1,0,1] is same for all parameters.
2) The dimension of 'systrans' will also change. If I add 1 extra for loop to this I have to change from my current systrans(:,:,j,i) to
systrans(:,:,k,j,i)
which I want to avoid and automate.
3) Last question, I want plot values stored in 'Svals' over parameter range. When I had only one parameter 'rho1' I did this:
plot(rho_grid,reshape(Svals(i,i,:),[1,length(rho_grid)]));
which gave me 2D plot. for 2 parameters case, I want to get a 3D plot and see the variation of entries of Svals over rho1 and rho2. How can I do that?
Thanks a lot for your help.

Sign in to comment.


Sindar
Sindar on 22 Jan 2020
check out griddedInterpolant, I think it'll do what you want
  2 Comments
Sindar
Sindar on 30 Oct 2020
interpn (or interp3 with same syntax):
% load in you data; you'll need to adjust these two lines
d=load('data.mat');
V=data.data;
% get number of points in each dimension, should be 80000,72,14
[Nx,Ny,Nz]=size(V);
% define a uniformly-spaced grid
[x,y,z] = ndgrid(1:Nx,1:Ny,1:Nz);
% Now, create the query grid with double the points in Y and Z:
[xq,yq,zq] = ndgrid(1:Nx,1:0.5:Ny,1:0.5:Nz);
% Interpolate V at the query points.
Vq = interpn(x,y,z,V,xq,yq,zq);
% Save the new data to a mat-file
save('data_finer.mat','Vq','xq','yq','zq')

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!

Translated by