简体   繁体   English

MATLAB,具有变化的第二维的三维矩阵

[英]MATLAB, three dimensional matrix with varying second dimension

I need a 3 dimensional matrix but the first dimensions are not the same. 我需要一个3维矩阵,但第一个维不相同。 So I have say NxT1 (N by T1) , NxT2 NxT3 and NxT4. 所以我说NxT1(N by T1),NxT2 NxT3和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. T2 = [50,100,150,200],因此我的3维矩阵将包含针对4种不同时间规格的N个模拟。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM