MATLAB每次迭代更改矩阵的名称 [英] MATLAB Changing the name of a matrix with each iteration

查看:437
本文介绍了MATLAB每次迭代更改矩阵的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道在每次迭代后用不同的名称存储矩阵是否干净?我希望能够以不同的名称存储每个矩阵(uMatrix),具体取决于我在哪个模拟,例如Sim1,Sim2,......等。首先运行后Sim1 = uMatrix,然后Sim2 = uMatrix第二次跑。因此,每次我可以为每个不同的模拟存储不同的uMatrix。

I was just wondering if there is a clean way to store a matrix after each iteration with a different name? I would like to be able to store each matrix (uMatrix) under a different name depending on which simulation I am on eg Sim1, Sim2, .... etc. SO that Sim1 = uMatrix after first run through, then Sim2 = uMatrix after 2nd run through. SO that each time I can store a different uMatrix for each different Simulation.

任何帮助都会非常感激,如果这是一个愚蠢的问题,那就很抱歉。此外,关于是否可以清理此代码的任何指示都很棒

Any help would be much appreciated, and sorry if this turns out to be a silly question. Also any pointers on whether this code can be cleaned up would be great too

我在下面使用的代码

d = 2;            
kij = [1,1];
uMatrix = [];
RLABEL=[];
SimNum = 2;

for i =1:SimNum
    Sim = ['Sim',num2str(i)] %Simulation number
    for j=1:d
        RLABEL = [RLABEL 'Row','',num2str(j) ' '];
        Px = rand;
        var = (5/12)*d*sum(kij);
        invLam = sqrt(var);
        u(j) = ((log(1-Px))*-invLam)+kij(1,j);
        uMatrix(j,1) = j;
        uMatrix(j,2) = u(j);
        uMatrix(j,3) = kij(1,j);
        uMatrix(j,4) = Px;
        uMatrix(j,5) = invLam;
        uMatrix(j,6) = var;
    end
    printmat(uMatrix,'Results',RLABEL,'SECTION u kij P(Tij<u) 1/Lambda Var')
end


推荐答案

确实有太多选择。要描述将数据放入并获取以下几种方法的数据:

There are really too many options. To go describe both putting data into, and getting data our of a few of these methods:

以变量名称编码

我真的非常不喜欢这种方法,但它似乎是你特别要求的。要将 uMatrix 保存为变量 Sim5 (在第5次运行之后),请在代码末尾添加以下内容:循环:

I really, really dislike this approach, but it seems to be what you are specifically asking for. To save uMatrix as a variable Sim5 (after the 5th run), add the following to your code at the end of the loop:

eval([Sim ' = uMatrix;']);  %Where the variable "Sim" contains a string like 'Sim5'

访问数据

listOfStoredDataNames = who('Sim*')
someStoredDataItem = eval(listOfStoredDataNames {1})  %Ugghh
%or of you know the name already
someStoredDataItem = Sim1;

现在,请不要这样做。让我试着说服你有更好的方法。

Now, please don't do this. Let me try and convince you that there are better ways.

使用结构

做同样的事情,使用一个名为(例如)的结构 simResults

To do the same thing, using a structure called (for example) simResults

simResults.(Sim) = uMatrix;

甚至更好

simResults.(genvarname(Sim)) = uMatrix;

访问数据

listOfStoredDataNames = fieldnames(simResults)
someStoredDataItem = simResults.(listOfStoredDataNames{1})
%or of you know the name already
someStoredDataItem = simResults.Sim1

这可以避免总是有问题的 eval 语句,更重要的是使附加代码更容易编写。例如,您可以轻松地将所有 simResults 传递到函数中以进行进一步处理。

This avoids the always problematic eval statement, and more importantly makes additional code much easier to write. For example you can easily pass all simResults into a function for further processing.

使用地图

要使用地图执行相同的存储,请先初始化地图

To use a map to do the same storage, first initialize the map

simResults = containers.Map('keyType','char','valueType','any');

然后在每次迭代时将值添加到地图

Then at each iteration add the values to the map

simResults(Sim) = uMatrix;

访问数据

listOfStoredDataNames = simResults.keys
someStoredDataItem = simResults(listOfStoredDataNames{1})
%or of you know the name already
someStoredDataItem = simResults('Sim1')

地图在字符串中更灵活,可用于名称,如果你感觉舒服,可能是一个更好的解决方案。

Maps are a little more flexible in the strings which can be used for names, and are probably a better solution if you are comfortable.

使用单元格数组

简单来说,没有废话存储的结果

For simple, no nonsense storage of the results

simResults{i} = uMatrix;

访问数据

listOfStoredDataNames = {};  %Names are Not available using this method
someStoredDataItem = simResults{1}

或者,使用略微无意义的

Or, using a slight level of nonesense

simResults{i,1} = Sim;      %Store the name in column 1
simResults{i,2} = uMatrix;  %Store the result in column 2

访问数据

listOfStoredDataNames = simResults(:,1)
someStoredDataItem = simResults{1,2}

这篇关于MATLAB每次迭代更改矩阵的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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