How to store multiple iterations of a 3x3 matrix using a for loop.

21 vues (au cours des 30 derniers jours)
N/A
N/A le 29 Juin 2022
Modifié(e) : Voss le 29 Juin 2022
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)]

Réponses (1)

Voss
Voss le 29 Juin 2022
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.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by