以下代码可以满足我的需求,我无法找到更有效的方法。基本上我只需要沿着5D矩阵的1维插值。有人可以请教我如何更有效地在MATLAB中做到这一点吗?谢谢!
% Define Parameters
BETA = 11000;
Desired_SOC = 0.55;
u1_length = 15;
u2_length = 3;
u3_length = 2;
x2_length = 2;
x3_length = 6;
x1_grid = [0.4:0.05:0.8]';
% Not really my data, but it will sufice
table_x1 = repmat(x1_grid,[1, x2_length,x3_length,u1_length,u2_length,u3_length]);
J_STAR = BETA*(x1_grid - Desired_SOC).^2;
tic;
SOC_State_Penalty = single(zeros(x1_length,x2_length,x3_length,u1_length,u2_length,u3_length));
for x2 = 1:x2_length
for x3 = 1:x3_length
for u1 = 1:u1_length
for u2 = 1:u2_length
for u3 = 1:u3_length
F = griddedInterpolant(x1_grid,J_STAR(:,x2,x3),'linear'); % Just need to penalize the SOC
SOC_State_Penalty(:,x2,x3,u1,u2,u3) = F(table_x1(:,x2,x3,u1,u2,u3));
end
end
end
end
end
toc;