0

I need a 3 dimensional matrix but the first dimensions are not the same. So I have say NxT1 (N by T1) , NxT2 NxT3 and NxT4. and I want to put them in one matrix so can I loop through each dimension. Here is my code:

y2(:,:,1) = zeros(N,T2(:,1));
y2(:,:,2) = zeros(N,T2(:,2));
y2(:,:,3) = zeros(N,T2(:,3));
y2(:,:,4) = zeros(N,T2(:,4));
y2(:,1,:) = c/(1-rho);

for z=1:size(T2,2)
    for i=2:T2(:,z)
        for j=1:N
            y2(j,i,z) = y2(j,i-1)+randn;
        end
    end
end

I want random walks for different time horizons basically. T2=[50,100,150,200] so my 3 dimensional matrix would contain N simulations for 4 different time specifications.

  • Perhaps not suitable for this forum. There are specialised MATLAB forums that you might consider posting to. – Luca Mar 26 '14 at 12:32
  • This question appears to be off-topic because it is not about statistics, data analysis, or machine learning. – whuber May 02 '14 at 15:05

1 Answers1

3

I think what you want is an array, not a matrix.

c = 1.0;
rho = 0.5;
N = 100;
T2 = [50, 100, 150, 200];
for i = [1:length(T2)];
    y2{i} = zeros(N, T2(i));
    y2{i}(1,:) = c/(1-rho);
end;

for i = [1:length(T2)];
    for j = [2:N];
        for k = [1:T2(i)];
            y2{i}(j,k) = y2{i}(j-1,k) + randn()
        end;
    end;
end;
Blink
  • 1,444
  • 5
  • 17
  • 25