want to use output matrix obtained for every iteration of a for loop in another for loop.

38 vues (au cours des 30 derniers jours)
J
J le 14 Avr 2024 à 20:03
Commenté : J le 16 Avr 2024 à 4:39
hi, i am developing a code where i have 2 functions: the first function gives me a smaller matrix 'k'.
i want to obtain this 'k' matrix for every iteration.
then i want to add these smaller matrices into a larger one, for that i am using the second for loop.
can anyone please guide me on how i can save each 'k' matrix after every iteration, and then use the 'k' matrices and add them to a larger matrix 'K'?
  6 commentaires
Torsten
Torsten le 15 Avr 2024 à 19:37
posr = 4;
posc = 3;
m = rand(6);
M = zeros(120);
M(posr:posr+5,posc:posc+5) = m
M = 120x120
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5457 0.9695 0.6962 0.8960 0.2307 0.4787 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.4676 0.2877 0.9645 0.1411 0.8091 0.1535 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.7857 0.8168 0.4157 0.1047 0.8180 0.2973 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.4923 0.7581 0.6293 0.9764 0.0582 0.7979 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1755 0.0105 0.4215 0.0759 0.2622 0.8527 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5153 0.6940 0.0167 0.7548 0.9984 0.7890 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
J
J le 16 Avr 2024 à 4:39
thank you so much for your help!!

Connectez-vous pour commenter.

Réponse acceptée

the cyclist
the cyclist le 14 Avr 2024 à 22:47
Here are two possibilities:
% Save into cell arrays
nsmall = 4;
mcell = cell(nsmall,1);
for k = 1:nsmall
mcell{k} = rand(3); % Note curly brackets, to save as *contents* of the cell
end
% Save each matrix as a "slice" of a three-dimensional array
nsmall = 4;
m3d = zeros(3,3,nsmall);
for k = 1:nsmall
m3d(:,:,k) = rand(3); % Saving each slike
end

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by