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

2 次查看(过去 30 天)
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 个评论
Torsten
Torsten 2024-4-15
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>

请先登录,再进行评论。

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by