Data saving for a loop

5 vues (au cours des 30 derniers jours)
NERTILA OGRENI
NERTILA OGRENI le 23 Jan 2021
Commenté : NERTILA OGRENI le 26 Jan 2021
I have a code like this:
for i=1:n
%calculuse
for j=1:n
%calculus
for k=1:n
%calculus
Save results in a matrix
end
end
end
I want to save the matrix inside a bigger matrix with matrix elements for each loop inside dhe interioe loop (k=1:k)
Thank you in advance

Réponse acceptée

William
William le 25 Jan 2021
Nertila -- Yes, that is what this code does. Maybe it would be more clear if I wrote it in this way, with the k-index being the outer loop rather than the inner loop. In each iteration of the k-loop, an S(i,j) matrix is computed in the i and j loops, and then stored in a(k,:,:).
a = zeros(n,n,n);
S = zeros(n,n);
for k = 1:n
for i = 1:n
%calculus
for j = 1:n
calculus
S(i,j) = ...
end
end
a(k,:,:) = S;
end
  1 commentaire
NERTILA OGRENI
NERTILA OGRENI le 26 Jan 2021
Thank you very much!

Connectez-vous pour commenter.

Plus de réponses (1)

William
William le 23 Jan 2021
a = zeros(n,n,n)
for i=1:n
%calculus
for j=1:n
%calculus
for k=1:n
%calculus
a(i,j,k) = result(k)
end
end
end
  2 commentaires
NERTILA OGRENI
NERTILA OGRENI le 25 Jan 2021
Can i do the same if i want to save matrixes as the element of the a?
NERTILA OGRENI
NERTILA OGRENI le 25 Jan 2021
I mean I have a S(i,j) matrix wich is calculatet k times inside the interior loop. I want to save it in a result(i,j,k with elements these S (i,j)

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by