【问题标题】:Creating matrix of output from script (matlab)从脚本创建输出矩阵(matlab)
【发布时间】:2012-10-16 23:56:07
【问题描述】:

我有一个类似下面的脚本:

for 
(do something)
end

还有使用来自循环的数据输出的输出(每次都会改变——当脚本运行时):

A = 1
A = 1.5

等等

我希望将每次都会更改的输出存储到矩阵中。 这可行吗?

for number of iterations
(Call script)
end
Output to excel

我想将数据存储到矩阵中的原因是能够一次将所有答案(多次迭代)输出到 Excel 中。

编辑: 为了更好地了解我的输出看起来像这样

Output = [rand() rand() rand(); rand() rand() rand()];

然后我用它来创建一个新变量:

var = Output(1,1)./Output(2,1); 

每次我运行脚本时,答案都会改变。每次这个新答案都是我希望保存在矩阵中的内容。希望能解决问题。

【问题讨论】:

    标签: excel matlab matrix output


    【解决方案1】:

    根据每个循环的输出/输出类型,您可以轻松地将中间结果保存在许多 MATLAB 数据结构中的一个中(randn 在下面用作“做某事”的示例):

    nIterations = 10;
    
    % scalar output
    A = zeros(1, nIterations);
    for n=1:nIterations
        A(n) = randn;
    end
    
    % matrix ouput of possibly changing size
    B = cell(1, nIterations);
    for n=1:nIterations
        B{n} = randn(1, n+1);
    end
    
    % matrix output of fixed size
    C = zeros(3, 3, nIterations);
    for n=1:nIterations
        C(:,:,n) = randn(3, 3);
    end
    

    【讨论】:

    • 感谢您的回答。我正在尝试将其应用于我的案例。为了更好地了解我的输出看起来像这样 Output = [rand() rand() rand();兰德()兰德()兰德()];然后我用它来创建一个新变量: var = Output(1,1)./Output(2,1);每次我运行脚本时,答案都会改变。每次这个新答案都是我希望保存在矩阵中的内容。希望能解决问题。
    【解决方案2】:

    假设 var 是您希望在每次迭代后放入矩阵的内容,我建议如下: 在代码周围添加另一个 for 循环,例如循环 i,然后最后不要将值分配给 var,而是分配给 var(i)。

    根据您的输出,您需要选择 var 的变量类型,例如单元格或矩阵。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 2015-12-21
      • 1970-01-01
      相关资源
      最近更新 更多