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

1 view (last 30 days)
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

Accepted Answer

Voss
Voss on 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

More Answers (1)

Taylor
Taylor on 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 Comments

Sign in to comment.

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!