Matlab: Matrix with variable number of of columns

1.2k Views Asked by At

in Matlab, I want to define a Matrix A showing portfolio weights over a number of time periods N. Each period has one column in the matrix, so it needs to have N columns, and I need to code this elegantly. So what I want is, for example if N=3:

A=[w(1),w(2),w(3)]

The vectors w(j) will already have been defined. I tried something like

A=@(N)([w(1):w(N)])

But it didn't work, and I have no idea how to reach it.

1

There are 1 best solutions below

2
On BEST ANSWER

If each $w(i)$ is stored as a function, there is no elegant way to build your matrix $A$ without a for loop.

Given: N
       M - number of elements in each w(j)
A = zeros(M,N);

for i =1:N
    A(:,i) = w(i);
end