Is there any way to create array of arrays or matrix of matrices?

i want to create array of arrays or matrix of matrices?
or
create arrays automatically in{ for - loop} then combine them in one array .

 Accepted Answer

Arrays can contain all types of Matlab variables as elements, but no arrays.
A "matrix of matrices" can be seen as 4D array:
a = zeros(2, 2, 3, 4);
a(:, :, 1, 1) = rand(2, 2);
etc.
Or with a loop:
for i1 = 1:3
for i2 = 1:4
a(:, :, i1, i2) = rand(2, 2);
end
end

6 Comments

i want to combine these N arrays as 1D array (vector)
i use
A= cat(2, X1,X2,Xn)
X1,X2 ,Xn , A are 1D arrays (vector). but i cant generate X1,X2,Xn automatically and they are 1D arrays(vector) how can i do that using this technician
Has this got anything to do with your original question?
cat(2, X1(:), X2(:), Xn(:))
If you want to use a loop, then depending on the value of n and the size of your arrays it might be a good idea to preallocate and directly assign to the right interval. What is your purpose?
what do u mean by "preallocate and directly assign to the right interval" example or demo please.
my problem is my code generate a lot of data and i done know how much they are, they change every time i run my program with different input. I want to split these data in several arrays .each array has part of data so my program runs fast because if i save them in one array the program become very very very very slow . and at last combine them in one array
You could use cell arrays
numSim = 10;
your_cell = cell(numSim,1);
%Generating data and storing it in a cell array
for ii = 1:numSim
temp_mat = randi(100,randi(10,1,2));
your_cell{ii} = temp_mat;
end
%Getting all data in a vector
your_result = cell2mat(cellfun(@(x) x(:),your_cell,'uniformoutput',false));
@aboomnea: Is the original problem solved?
thanks very much @jan-simon:
it solve my problem

Sign in to comment.

More Answers (0)

Categories

Asked:

on 15 May 2013

Community Treasure Hunt

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

Start Hunting!