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

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

问题描述

我只是想知道在每次迭代后是否有一种干净的方法来存储具有不同名称的矩阵?我希望能够将每个矩阵 (uMatrix) 存储在不同的名称下,具体取决于我所在的模拟,例如 Sim1、Sim2、.... 等.所以 Sim1 = uMatrix 在第一次运行后,然后 Sim2 = uMatrix2 次运行.这样每次我都可以为每个不同的模拟存储不同的 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}

或者,使用一点废话

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天全站免登陆