Extract several matrices from single column vector

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 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 10 月 24 日
What's the objective here? Why do you need to store matrices and extract them with the same shape?
Have you considered storing the matrices in a cell array?
Cedric Kotitschke
Cedric Kotitschke 2023 年 10 月 24 日
So basically I'm trying to create parametrized linear models.
The way I do this is by using first-order taylor approximations. For this, I need to compute the jacobian of all entries in the A,B,C,D matrices with regards to the parameters.
This is why I need to flatten them, so that I have a function outputtting a single vector.
J = fun(p)
J_tilde = J0 + dJ * (p - p0)
In the end, I want to construct a linear system, so I need to extract A,B,C,D from J. However, I want this system to be tunable, so the vector p is a genmat object which means that J is also a genmat object.
The problem is that reshaping and matrix indexing is somehow buggy for genmat objects. This is why I need a different approach
Bruno Luong
Bruno Luong 2023 年 10 月 24 日
"The problem is that reshaping and matrix indexing is somehow buggy for genmat objects"
Please show an example.
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 24 日
I guess you can not convert to double and then do the operations.
Have you considered storing the (genmat) matrices in a cell array?
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 日

0 投票

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 件)

製品

リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by