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

1 次查看(过去 30 天)
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

采纳的回答

Voss
Voss 2024-3-21
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

更多回答(1 个)

Taylor
Taylor 2024-3-21
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 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by