Matlab: Matrix with variable number of of columns

$\begingroup$

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.

$\endgroup$ 4

1 Answer

$\begingroup$

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
$\endgroup$ 2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like