Associate an array to every element of a matrix

How I can associate an array to every element of a matrix?
For example, i have a 2x2 Matrix. Now Matrix(1,1) must be an array, is it possible? How can I declare such multidimensional array?
Thanks in advance!

 Accepted Answer

Is there a reason why you cannot simply use a 3-dimensional array?
Thus, if A has dimensions 2x2xn, then it can store n such 2x2 matrices. Access the k'th such matrix as
A(:,:,k)
Only if your individual arrays are not all the same size do you need to use a more complicated storage scheme such as a cell array. Admittedly, a cell array is trivial to use and create.

3 Comments

No, I could use a three dimensional array.
But, every element of the matrix must point to an array. I just don't know how to do it!
Given a matrix: M = [2 1; 3 4]; I need something like: M(1,1) = [1 2 3]; M(1,2) = [3 4 2]; And so on.
Thanks!
John's suggestion is good, if all your vectors are the same length:
% Define initial 2-D array.
M = [2 1; 3 4];
% I need something like: M(1,1) = [1 2 3]; M(1,2) = [3 4 2];
% Add on two more layers. Assumes all vectors to add have the same length.
M(end, end, 3) = 0
M(1,1,:) = [1 2 3];
M(1,2,:) = [3 4 2];
% Print to command window:
M
% To extract the first vector at M(1,1):
vector1 = squeeze(M(1, 1, :))
If they're not all the same length, you can use a cell array or a structure array.
They are all the same length, thanks!

Sign in to comment.

More Answers (1)

Use cell array. For example
M={ [1 2 3] [1 2] [4 5;6 5] 'abc' [ ] }

Categories

Tags

Community Treasure Hunt

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

Start Hunting!