Extract several matrices from single column vector

2 ビュー (過去 30 日間)
Cedric Kotitschke
Cedric Kotitschke 2023 年 10 月 24 日
回答済み: Cedric Kotitschke 2023 年 10 月 24 日
Hey,
I have three matrices
A, B, C, D which I flatten to a single column vector:
J = [A(:); B(:); C(:); D(:)]
How can I used indexing to extract the matrices again? Unfortunately, I cannot use reshape because J is a vector of realp objects.
Basically I want to do this:
A = J(idxA);
B = J(idxB);
C = J(idxC);
D = J(idxD);
Thanks for your help!
  6 件のコメント
Cedric Kotitschke
Cedric Kotitschke 2023 年 10 月 24 日
n = 20;
% Create genmat object
pGenmat = [];
for i = 1:n
pGenmat = [pGenmat; realp(sprintf('p%d', i), i)];
end
% Create double array with same size
pDouble = reshape(1:n, n, 1);
% Reshape double array (no error)
pDouble_reshaped = reshape(pDouble, 4, 5);
% Reshape genmat (error)
pGenmat_reshaped = reshape(pGenmat, 4, 5);
Bruno Luong
Bruno Luong 2023 年 10 月 24 日
I don't know genmat objects. I'll delete my answer.

サインインしてコメントする。

採用された回答

Cedric Kotitschke
Cedric Kotitschke 2023 年 10 月 24 日
For everyone who also struggles with this, here's my solution. It is clunky but that's only because genmat objects are buggy when it comes to reshaping and matrix indexing.
The input to this example are the parameterset, which holds names, values and nominal values of some parameters and the function handle which includes trimming and linearizing of a simulink model, given the parameterset.
% Utility function
function [J, sys] = getSys(x, parameterset, fun)
parameterset.Values = x;
sys = fun(parameterset);
J = [sys.A(:); sys.B(:); sys.C(:); sys.D(:)];
end
% Get nominal system
[J0, sys0] = getSys(parameterset.NominalValues, parameterset, fun);
% Compute jacobian
f = @(x) getSys(x, parameterset, fun);
x0 = parameterset.NominalValues;
jac = roughjacobianest(f, x0);
% Create tunable parameters
p = [];
for param = parameterset.Elements
p = [p; realp(param.Name, param.NominalValue)];
end
% Create linear system
J = J0 + jac * (p - parameterset.NominalValues(:));
% This workaround is necessary because both reshaping and proper
% indexing is buggy when it comes to genmat objects
na = numel(sys0.A);
szA = size(sys0.A);
Ja = J(1:na);
nb = numel(sys0.B);
szB = size(sys0.B);
Jb = J((na+1):(na+nb));
nc = numel(sys0.C);
szC = size(sys0.C);
Jc = J((na+nb+1):(na+nb+nc));
nd = numel(sys0.D);
szD = size(sys0.D);
Jd = J((na+nb+nc+1):(na+nb+nc+nd));
A = genmat(zeros(szA));
for i = 1:na
[idxRow, idxCol] = ind2sub(szA, i);
A(idxRow, idxCol) = Ja(i);
end
B = genmat(zeros(szB));
for i = 1:nb
[idxRow, idxCol] = ind2sub(szB, i);
B(idxRow, idxCol) = Jb(i);
end
C = genmat(zeros(szC));
for i = 1:nc
[idxRow, idxCol] = ind2sub(szC, i);
C(idxRow, idxCol) = Jc(i);
end
D = genmat(zeros(szD));
for i = 1:nd
[idxRow, idxCol] = ind2sub(szD, i);
D(idxRow, idxCol) = Jd(i);
end
sys = ss(A, B, C, D);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTime and Frequency Domain Analysis についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by