Associate an array to every element of a matrix

1 view (last 30 days)
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

John D'Errico
John D'Errico on 30 Jul 2016
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
Image Analyst
Image Analyst on 31 Jul 2016
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.

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 30 Jul 2016
Edited: Azzi Abdelmalek on 30 Jul 2016
Use cell array. For example
M={ [1 2 3] [1 2] [4 5;6 5] 'abc' [ ] }

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!