Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
Sergio
Sergio am 21 Mär. 2024
Kommentiert: Stephen23 am 21 Mär. 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

Akzeptierte Antwort

Voss
Voss am 21 Mär. 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

Weitere Antworten (1)

Taylor
Taylor am 21 Mär. 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 Kommentare
Sergio
Sergio am 21 Mär. 2024
Bearbeitet: Sergio am 21 Mär. 2024
reshape(M, 2, 2)
but how to call on the grad values?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by