George Tseres
George Tseres

Reputation: 498

Creating a container of matrices with different dimensions

I would like to make multiple arrays with different dimensions in Matlab. Is there a way to do this inside a "for" loop?

For example, I would like to create a matrix A with dimensions 100x100, then with 200x200,etc

Upvotes: 1

Views: 2108

Answers (2)

Tiddo
Tiddo

Reputation: 6554

You could create a cell in which you store the matrices:

a = cell(10,1);
for n=1:10
    a{n} = zeros(n*100,n*100);
end

Note: to get an item from a cell you should use {} instead of (). a{1} returns the first matrix, a(1) returns a cell which contains that matrix.

http://www.mathworks.nl/help/techdoc/ref/cell.html

Upvotes: 4

user1087001
user1087001

Reputation:

Try using the zeros function instead of creating your array inside of a loop.

Something like:

B = zeros(m,n)
A = zeros(m,n)

This will be much faster initially (since the array is not having to be resized every time you add an element); you can then iterate over it later and add whatever values you need.

EDIT: I should clarify, the zeros function creates an m X n matrix (or an array if you leave off the second argument) and fills it with all zeros. It's a good starting point for constructing large arrays.

Upvotes: 2

Related Questions