Extract several matrices from single column vector

2 vues (au cours des 30 derniers jours)
Cedric Kotitschke
Cedric Kotitschke le 24 Oct 2023
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 commentaires
Cedric Kotitschke
Cedric Kotitschke le 24 Oct 2023
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 le 24 Oct 2023
I don't know genmat objects. I'll delete my answer.

Connectez-vous pour commenter.

Réponse acceptée

Cedric Kotitschke
Cedric Kotitschke le 24 Oct 2023
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);

Plus de réponses (0)

Catégories

En savoir plus sur Time and Frequency Domain Analysis dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by