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

2 views (last 30 days)
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 Comments
Torsten
Torsten on 15 Apr 2024
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>

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 14 Apr 2024
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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!