How can I increase the size of matrices in cell arrays with each iteration of a loop?

조회 수: 8 (최근 30일)
I have a cell array that is 10x10 with each cell containing a 1x3 matrix of numerical values. This cell array is generated inside a loop that has 10 iterations, thus there are 10 versions of this cell array, each with different values.
I would like to know how to store each iteration of this cell array in a single cell array which is still 10x10, however with each cell containing a 10x3 matrix. Basically, I want each iteration of the loop to add a row to the matrix in each cell and populate those rows with the corresponding cells of the new cell array generated during that iteration.

채택된 답변

Voss
Voss 2023년 2월 7일
A 10x10 cell array where each cell contains a 1x3 vector:
C = repmat({[0 0 0]},10,10);
Another 10x10 cell array where each cell contains a 1x3 vector:
C_new = repmat({[1 1 1]},10,10);
Append the contents of each cell of C_new as a new row on the contents of the corresponding cell of C:
C = cellfun(@vertcat,C,C_new,'UniformOutput',false)
C = 10×10 cell array
{2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double}
So that last line is what you would do on each iteration, where C_new is the new version each time.
% Initialize C
C = cell(10,10);
% run 10 iterations
for ii = 1:10
% ...
% here you would calculate C_new, which is the new version for this iteration.
% ...
C = cellfun(@vertcat,C,C_new,'UniformOutput',false);
end
disp(C)
{10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double} {10×3 double}

추가 답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 2월 7일
There are a few different ways to create such as cell array. If to get it done in a loop:
rng(1) % To replicate at any time and obtain the same results of random numbers
for ii = 1:10
for k=1:10
H{ii,k} = randi([-5,5],1, 3);
end
end
H{1,1}
ans = 1×3
-1 2 -5
H{1,2}
ans = 1×3
-2 -4 -4
H{3,1}
ans = 1×3
-4 -1 2
% etc

Fifteen12
Fifteen12 2023년 2월 7일
This code gives two solutions, one that can be implemented after your original iteration (it just harvests the values and puts them into a new matrix), and one that can be implemented during your original iteration (which you'll have to splice into your own code).
% Generate original cell arrays
n = 10;
for k = 1:n
for i = 1:n
for j = 1:n
foo{i, j} = [i, j, k]; %Random values
end
end
original{k} = foo;
end
% Post adaptation from original iteration
for i = 1:n
for j = 1:n
temp = zeros(n, 3);
for k = 1:length(original)
temp(k, :) = original{k}{i, j};
end
out1{i, j} = temp;
end
end
% Adaption during original iteration
out2 = cell(n, n);
for k = 1:n
for i = 1:n
for j = 1:n
out2{i, j}(k, :) = [i, j, k]; %Random values
end
end
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by