MATLAB中的double for循环,用于存储信息 [英] Double for loop in MATLAB, storing the information

查看:345
本文介绍了MATLAB中的double for循环,用于存储信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB中有两个for循环.

I have two for loops in MATLAB.

其中一个for循环导致将不同的变量插入到模型中,这些变量分别为43,然后有5个层.

One of the for loops leads to different variables being inserted into the model, which are 43 and then I have 5 horizons.

因此,我估计该模型为215次.

So I estimate the model 215 times.

我的问题是我要将其存储在215x5矩阵中,我拥有x5的原因是我估计了5个变量,其中4个是固定的,而另一个则来自for循环.

My problem is I want to store this in 215x5 matrix, the reason I have x5 is that I am estimating 5 variables, 4 are fixed and the other one comes from the for loop.

我已经尝试通过两种方式做到这一点,

I have tried to do this in two ways,

首先,我创建一个名为

out=zeros(215,5);

第一个for循环是

for i=[1,2,3,4,5];

第二个for循环是

for  ii=18:60;

18:60是我如何使用XLS读取来定义变量的方法,例如它们作为(data:,ii)插入到模型中.

The 18:60 is how I define my variables using XLS read, e.g. they are inserted into the model as (data:,ii).

我试图以两种方式存储数据,我想存储包含五个估算值的OLS

I have tried to store the data in two ways, I want to store OLS which contains the five estimates

首先,

out(i,:)=OLS;

此方法创建一个5 x 5矩阵,其中每个水平线的估计值为(18:60)之一.

This method creates a 5 x 5 matrix, with the estimates for one of the (18:60), at each of the horizons.

第二,

out(ii,:)=OLS;

这仅在一个范围内存储每个变量(18:60)的变量.

This stores the variables for each of the variables (18:60), at just one horizon.

我想有一个矩阵,用于存储我的每个年龄段(18:60)的所有估计值OLS.

I want to have a matrix which stores all of the estimates OLS, at each of the horizons, for each of my (18:60).

最小示例

clear;

for i=[1,2,3,4,5];
    K=i; 
    for  ii=18:60
        x=[1,2,3,i,ii];
        out(i,:)=x;       
    end
end

因此变量out将存储1 2 3 5 60

So the variable out will store 1 2 3 5 60

我希望变量存储所有组合

I want the variable out to store all of the combinations

1 2 3 1 1
1 2 3 1 2
  ...
1 2 3 5 60

谢谢

推荐答案

正如您所发现的,仅使用循环变量之一对输出结果进行索引会导致大多数结果被覆盖,仅保留最终结果相关循环的迭代.

As you've discovered, using just one of your loop variables to index the output results in most of the results being overwritten, leaving only the results from the final iteration of the relevant loop.

有两种创建索引变量的方法.

There are 2 ways to create an indexing variable.

1-您可以使用一个自变量,该变量在循环之前初始化,并在内部循环结束时递增.

1- You can use an independent variable, initialised before the loops and incremented at the end of the internal loop.

kk=1; 
for i=1:5
    for ii=18:60
        %calculate OLC
        out(kk,:)=OLC;
        kk = kk+1;
    end 
end

2-使用iii

kk = i + 5*(ii-18)  

(像以前一样在循环中使用,没有增量)

(Use in loop as before, without increment)

这篇关于MATLAB中的double for循环,用于存储信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆