How to generate a matrix from specific outputs from a MATLAB file?

1 vue (au cours des 30 derniers jours)
Sergio
Sergio le 21 Mar 2024
Commenté : Stephen23 le 21 Mar 2024
Hi, the file test_modified.m generates a set of values for "SP" and "grad". How can I extract the calculated grad values which appear as:
2.1146
1.5751
1.5751
2.7125
and form a matrix as
2.1146 & 1.5751
1.5751 & 2.7125
automatically?
Thanks

Réponse acceptée

Voss
Voss le 21 Mar 2024
Each grad is a 2x2 matrix; it's not clear from the question what size you want the final grad array to be, so here's one way to put them all in a 2x2xN array:
N = 4; % number of experiments
result = zeros(2*N,1); % pre-allocate result arrays
grad_all = zeros(2,2,N);
dt=0.01;
T=1;
k=[5;1];
y0_all = [2 1 1 4; 1 1 0 1];
for ii = 1:N
y0 = y0_all(:,ii);
[SP,grad]=enzyme(y0,k,dt,T);
result((ii-1)*2+[1 2]) = y0-SP;
grad_all(:,:,ii) = grad;
end
disp(grad_all)
(:,:,1) = -0.2244 0.5130 0.2244 -0.5130 (:,:,2) = -0.1137 0.3601 0.1137 -0.3601 (:,:,3) = -0.0548 0.2092 0.0548 -0.2092 (:,:,4) = -0.4028 0.5262 0.4028 -0.5262

Plus de réponses (1)

Taylor
Taylor le 21 Mar 2024
a = [2.1146 1.5751 1.5751 2.7125]'
a = 4×1
2.1146 1.5751 1.5751 2.7125
b = reshape(a,[2 2])
b = 2×2
2.1146 1.5751 1.5751 2.7125
  4 commentaires
Sergio
Sergio le 21 Mar 2024
Modifié(e) : Sergio le 21 Mar 2024
reshape(M, 2, 2)
but how to call on the grad values?
Stephen23
Stephen23 le 21 Mar 2024
" but how to call on the grad values?"
Just do the same for GRAD as I showed you for SP:
Then RESHAPE after the loop.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB Coder dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by