我想在下面的代码中将2x2单元格转换为简单的2x2矩阵。这样做,我面临标题中提到的错误。代码如下。
clc
close all
clear all
A = [0 1;-2 1];
B = [0;1];
Q = [2 6;6 5];
R = 0.25;
%x0 = [2;-3];
X0 = [1; 1; 1; 1];
Tf = 5;
tm = 0:0.1:Tf;
[T X] = ode45(@(t,X)mRiccati(t, X, A, B, Q, R), [0 Tf], X0)
[m n] = size(X);
P = mat2cell(X, ones(m,1), n);
fh_reshape = @(x)reshape(x,size(A));
P = cellfun(fh_reshape,P,'UniformOutput',false);
K= reshape(cat(1,P{:}),size(P))
function dXdt = mRiccati(t, X, A, B, Q, R)
X = reshape(X, size(A)); %Convert from "n^2"-by-1 to "n"-by-"n"
dXdt = A.'*X + X*A - X*B*inv(R)*B.'*X + Q; %Determine derivative
dXdt = dXdt(:); %Convert from "n"-by-"n" to "n^2"-by-1
end
错误是由于行“K =重塑(cat(1,P {:}),size(P))”。这有什么不对?