필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Sergio
Sergio 2024년 3월 21일
댓글: Stephen23 2024년 3월 21일
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
Sergio
Sergio 2024년 3월 21일
편집: Sergio 2024년 3월 21일
reshape(M, 2, 2)
but how to call on the grad values?
Stephen23
Stephen23 2024년 3월 21일
" 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.

댓글을 달려면 로그인하십시오.

카테고리

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