Suppose there is a 4-dimensional array A of size klmn. I want to obtain an array B of size KLmn by interpolating A along the first two dimensions. More precisely, what I want to do conceptually is in the code below. But how to make it more efficient avoiding the loops? Thank you.
failed to connect to server
failed to resolve hostname
**invalid user input**
request timeout
server returned a 500 response
socket hang-up
system is out of memory
答案 0 :(得分:1)
这就是我认为您打算做的事情(将您的代码更改为在原始矩阵的范围内进行插值):
% Initialization
k=10;l=10;m=10;n=10;
K=100; L=100;
B=NaN(K,L,m,n);
% Just for the sake of example:
vec_k=1:k;
vec_l=1:l;
vec_K=linspace(1,k,K);
vec_L=linspace(1,l,L);
A=rand(k,l,m,n);
% Interpolation
for ind_m=1:m
for ind_n=1:n
A_aux=squeeze(A(:,:,ind_m,ind_n));
B(:,:,ind_m,ind_n)=interp2(vec_k',vec_l,A_aux,vec_K',vec_L,'spline');
end
end
使用interpn
[in{1:4}]=ndgrid(vec_k,vec_l,1:m,1:n);
[out{1:4}]=ndgrid(vec_K,vec_L,1:m,1:n);
X=interpn(in{:},A,out{:},'spline');