Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

Code I am using below

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
393 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

Encode in variable names

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'

To access the data

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.

Use a structure

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

simResults.(Sim) = uMatrix;

or even better

simResults.(genvarname(Sim)) = uMatrix;

To access the data

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

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.

Use a Map

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;

To access the data

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.

Use a cell array

For simple, no nonsense storage of the results

simResults{i} = uMatrix;

To access the data

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

To access the data

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...