【问题标题】:Storing vectors and matrices of different sizes in loops在循环中存储不同大小的向量和矩阵
【发布时间】:2016-11-10 19:42:47
【问题描述】:

我想为我的算法正在执行的每个循环存储不同矩阵/向量大小的值。我的 MWE 如下:

for n1=1:T
 a1=f1(n1)  % Some expression which depends on n1
            % a1 is just a parameter value
for n2=1:T
  a2=f2(n2)   % Some expression which depends on n2
              % a2 is just a parameter value 

    while d<1
      algorithm=F(a1,a2,)  % my algorithm depends on these parameter values
    end 

     % In this step I want remember the combination of parameters and store  some results 

     v1= [a1 a2]  % I store the combination of parameters used in the while loop 

     [A,B,C]=somefunction() % some results given inputs from the while loop
      % A,B,C are matrices **of Different sizes** 
      v2 = [ 4x1 4x1 4x1]   % I store some column vectors 

   % What I basically want to do is  
   % to remember and SEE in some MATLAB convenient form the following: 
   % 1. for this combination of parameters in v1  
   % 2. I got A B C matrices and the matrix v2  

 end
end

谁能帮助我如何尽可能有效地做到这一点?理想情况下,我想在每次执行 for 循环时存储所有参数值和矩阵的名称,但我不知道这在 MATLAB 中是否可行。

【问题讨论】:

  • 如何将其保存为v1=[v1;a1 a2]v2 = [v2 ; 4*1 4*1 4*1]。 ?

标签: arrays matlab loops matrix cell


【解决方案1】:

考虑在结构中使用dynamic field references。这将允许您创建一个输出结构,其中包含循环中给定运行的结果和输入。使想法适应您的代码。

for n2=1:maxruns
%  Same code as above. 

     AlgorithmOutput.((strcat('Run',int2str(n2))).input.A1=a1
     AlgorithmOutput.((strcat('Run',int2str(n2))).input.A2=a2
     % this will store your inputs in something like AlgorithmOutput.Run1.input
     AlgorithmOutput.((strcat('Run',int2str(n2))).output.V1=v1;
     AlgorithmOutput.((strcat('Run',int2str(n2))).output.V2=v2;
     % this will store your inputs in something like AlgorithmOutput.Run1.output


end

这里需要注意的是,您需要确保以字母而不是数字开始动态引用(matlab 命名规则)。您还需要使用fields(AlogrithmOutput) 之类的东西,在这种情况下,它将返回一个单元数组,您可以随后对其进行迭代。 如果您有非常深入和复杂的输入和输出,这会导致一些问题,但这应该允许通过AlogrithmOutput.Run5.input.A1 等语法以直观的方式访问任何给定运行的输入/输出。

【讨论】:

  • 谢谢,但我有两个循环有关系吗?命令是否修改为:AlgorithmOutput.((strcat('Run',int2str(n1,n2))).input.A1=a1
  • 这仅取决于您希望如何存储数据AlgorithmOutput.(strcat('Run',int2str(n1))).(strcat('SubRun',int2str(n1))).input.A1=a1 将是处理它的一种方法(如果您的运行以某种逻辑方式分组)。如果您只想要一个巨大的顺序列表中的所有内容,那么您可以使用AlgorithmOutput.(strcat('Run',int2str(n1),int2str(n2))).input.A1。您可以在结构中使用动态字段嵌套任意多个级别,尽管访问起来会有些复杂。
  • 在您的第一个示例中,我认为您的意思是:AlgorithmOutput.(strcat('Run',int2str(n1))).(strcat('SubRun'‌​,int2str(n2))).input‌​.A1=a1n1 在两个int2str() 中都有。我说的对吗?
  • 没错!很棒的收获。
【解决方案2】:

根据您要查看的内容,单元结构将允许您非常方便地在一个结构中查看不同的数组大小/数据类型。

results = {};
for n1=1:T
   for n2=1:T

     ...
     results{n1,n2,1} = a1
     results{n1,n2,2} = a2
     results{n1,n2,3} = v1
     results{n1,n2,4} = v2
     results{n1,n2,5} = A
     results{n1,n2,6} = B
     results{n1,n2,7} = C
   end
end

这里提到的动态 struct.field 方法也很有效。您必须决定是伪数组方法(单元格)还是树方法(struct.field)最适合访问/查看/存储您的数据。

【讨论】:

  • 请注意,我有一个双循环,这是否意味着我可以将其更改为:` results{n2,n1,1} = a1`,例如?
  • 啊,错过了缩进。是的,这会奏效!三维单元格在变量资源管理器中不会那么容易查看,但会很容易访问。
猜你喜欢
  • 2016-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-09
相关资源
最近更新 更多