How to store multiple iterations of a 3x3 matrix using a for loop.
조회 수: 25 (최근 30일)
이전 댓글 표시
Below is a piece of my foor loop that I have created to generate Cpp_p_extrap (accoridng to rules not included in this). My goal is to create and store n iterations of the "Cpp_p_extrap" matrix. Im assuming storing a matrix as an interval is possible; however, I do not know how to do that. So, I have opted for stored each individual component of the Cpp_p_extrap matrix (a 3x3 matrix) inside the foor loop. Upon conclusion of the loop, I then combined the terms to create the Cpp_p_extrap(m,n) matrix, according to the indexing as shown.
My eventual goal is to store all the n 3x3 Cpp_p_extrap matrices. Is there a way to tweak this code to allow me to store all the Cpp_p_extrap matrices without coding recombining each element and storing them in individual matrices?
I appreciate any and all help with this.
t_0 = 0
t_finish = 120
n = 0
for int = t_0:1_t_finish
n = n + 1
%store Cpp_p_extrap(m,n) as individual components in vectors
Cpp_p_extrap_11(n) = Cpp_p_extrap(1,1)
Cpp_p_extrap_12(n) = Cpp_p_extrap(1,2)
Cpp_p_extrap_13(n) = Cpp_p_extrap(1,3)
Cpp_p_extrap_21(n) = Cpp_p_extrap(2,1)
Cpp_p_extrap_22(n) = Cpp_p_extrap(2,2)
Cpp_p_extrap_23(n) = Cpp_p_extrap(2,3)
Cpp_p_extrap_31(n) = Cpp_p_extrap(3,1)
Cpp_p_extrap_32(n) = Cpp_p_extrap(3,2)
Cpp_p_extrap_33(n) = Cpp_p_extrap(3,3)
end
% n extrapolations of Cpp_p_extrap stored as single matrix
Cpp_p_extrap_1 = [Cpp_p_extrap_11(1), Cpp_p_extrap_12(1),Cpp_p_extrap_13(1);
Cpp_p_extrap_21(1), Cpp_p_extrap_22(1),Cpp_p_extrap_23(1);
Cpp_p_extrap_31(1), Cpp_p_extrap_32(1),Cpp_p_extrap_33(1)]
댓글 수: 0
답변 (1개)
Voss
2022년 6월 29일
Use a 3-dimensional array of size 3-by-3-by-n:
t_0 = 0
t_finish = 120
n = 0
for int = t_0:t_finish
n = n + 1
Cpp_p_extrap_all(:,:,n) = Cpp_p_extrap;
end
% display result of 1st iteration
Cpp_p_extrap_all(:,:,1)
% display result of last iteration
Cpp_p_extrap_all(:,:,end)
% etc.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!