Storage of variables being generated in a loop
Show older comments
Hy guys!
My code uses random variables to generate times of up and down of components and feed a state map. and im using the sum of the times to calculate indexs.
I need to store theses indexes to make an average of them(10 is good enough)
Here's my code:
U=rand(n,1);
for i=1:n
%Primeiro Tup
T7=-1*log(U)/((lambdaT+lambdaP));
end
%0 = Up
%1= down transitorio
%2= down permanente
% Primeiro tempo, todos os componentes ON
Tclock7=zeros(1,n);
%Calcular o menor T para ver o componente que ira transitar de estagio
comp=find(T7==min(T7));
%Mudando estado do componente envolve sortear se caso esteja transitando
%de ON para OFF sera a falha permanente ou transitoria
if Tclock7(comp)==0
if rand>0.111
Tclock7(comp)=1;
mi=mit(comp);
else
Tclock7(comp)=2;
mi=mip(comp);
end
else
Tclock7(comp)=0
end
% Calculando Tdown do primeiro Componente off
T7(comp)=T7(comp)-1*log(U2)/((mi*trecho(comp))/horas);
% Encontrando próximo componente a transitar
comp=find(T7==min(T7));
%Mudando estado do componente
if Tclock7(comp)==0
if rand>0.111
Tclock7(comp)=1;
mi=mit(comp);
contT7=1;
contP7=0
else
Tclock7(comp)=2;
mi=mip(comp);
contP7=1;
contT7=0;
end
else
Tclock7(comp)=0
end
Tclock7=Tclock7;
T7=T7;
% Calculando novo Tup ou Tdown
% Essa rotina tem que ser automatizada até que todos os componentes cheguem
% a 8760 para fazer a avaliação dos criterios de disponibilidade de 1 ano
% Iteração para todos os componentes(17) chegarem a 1 ano de planejamento
%Variaveis de contagem
iter7=1;
contON7=0;
w=10;
%Loop para analisar todos os componentes
while length(S7) < 10
for all=1:n
%Loop para chegar até analise de 1 ano
while T7(all)<horas
iter7=iter7+1;
%Encontrar qual componente transita
comp=find(T7==min(T7));
%Caso o componente esteja DOWN
if Tclock7(comp)==1 | Tclock7(comp)==2
%Transicoes para ON
contON6=contON6+1;
%Calculo do TUP
T7(comp)=T7(comp) -1*log(rand)/((lambdaT+lambdaP));
%Faz a transiçao no mapa de estados
Tclock7(comp)=0;
% Caso o componente esteja UP
else Tclock7(comp)=0;
%Sorteio para ver se a falha sera transitoria ou permanente
if rand>0.111
% Puxo o mi transitorio
mi=mit(comp);
T7(comp)=T7(comp)-tmorto;
%Faz a transicao no mapa de estados
Tclock7(comp)=1;
% Contador de numero de falhas transitorias por ano
contT7=contT7+1;
%Puxo o mi permanente
else mi=mip(comp)
%Faz a transicao
Tclock7(comp)=2;
%Variavel aleatoria para calcular TDOWN
U3=rand;
%Contador de numero de falhas permanentes por ano
contP7=contP7+1;
end
%Calculo do TDOWN
T7(comp)=T7(comp) -1*log(U3)/((mi)*trecho(comp)/horas);
TDT7=(-1*log(U3)/((mi)*trecho(comp)/horas));
TDT7=TDT7+(-1*log(U3))/((mi)*trecho(comp)/horas);
% Indice de Frequencia
% SAIFI
consumidores=sum(consumidores);
saifi7=contP7/consumidores;
%SAIDI - indice de duraçao das interrupcoes sustentadas
saidi7=TDT7/consumidores;
% Indice de Confiabildidade
%ASAI
for m=1:10
asai7=(horas*n-TDT7)/(horas*n);
S7=[asai7(m)]
14 Comments
Rik
on 24 Nov 2019
Your code is not formatted correctly so it is difficult to follow the flow. Please edit your question to make it easier to read. Please also indicate what is exactly that you want to save.
Daniel Ribeiro Gonçalves
on 25 Nov 2019
Rik
on 25 Nov 2019
The code you posted has syntax issues, so it is impossible to run your code. There are also at least some variables missing.
Stephen23
on 4 Dec 2019
Daniel Ribeiro Gonçalves's "Answer" moved here and code uploaded as a file:
Well, here's my code, which is working.
What happens is: I need to generate indexes for every event, which is a year divided in hours. When all the elements in vector T1>horas, my variables needed for my indexes will be saved and the program will calculate my indexes(saifi1, saidi1, etc).
I need this while loop to be in another loop to be run 50 times, and to save in my indices1 matrix the indexes for each it bigger loop.
I hope I was clear this time.
Thnks
Rik
on 4 Dec 2019
There are a lot of issues with your code. A few examples:
- You are using eval. Don't do that, it will always lead to some obscure error or strange behavior that is hard to track down.
- You are using an array as the conditional for one of your while loops. That probably doesn't do what you think it does. Always explicitly make sure your conditional is a scalar (use either the all or any function to turn an array into a scalar).
- You're code was not alligned properly, making it harder to read. Select all your code and hit the 'smart indent' button in the toolbar (or use the ctrl-i hotkey).
- There are several m-lint warnings. Resolving them will generally improve your code. If you disagree with m-lint, right-click on the source of the warning and select the 'suppress warning on this line' option (or type %#ok behind the line of code).
If your goal is to store the values in indices1, why don't you follow my advice below of making it either a cell array, or a normal array?
You didn't create a MWE (because the source data is missing), so we are still not able to run your code.
Daniel Ribeiro Gonçalves
on 4 Dec 2019
Rik
on 4 Dec 2019
Look at the first few lines of your code: it is loading some file. Have you share that file here?
And what exactly is your issue implementing the solution I posted below?
Daniel Ribeiro Gonçalves
on 4 Dec 2019
Rik
on 4 Dec 2019
I have no clue what you mean by this block of unformatted code.
Daniel Ribeiro Gonçalves
on 4 Dec 2019
Rik
on 4 Dec 2019
The solution is actually the same as I suggested in my answer. Either use a cell:
%before loop:
indices1=cell(m,1);
%inside loop:
indices1{k} = [saifi1 saidi1 asai1 ENS1 AENS1 CAIFI1 CAIDI1];
Or use an array:
%before loop:
indices1=zeros(m,7);
%inside loop:
indices1(k,:) = [saifi1 saidi1 asai1 ENS1 AENS1 CAIFI1 CAIDI1];
Daniel Ribeiro Gonçalves
on 4 Dec 2019
Rik
on 4 Dec 2019
Since you refuse to provide working code and don't provide any details about what you mean with "Doesnt work" it is very hard to help you. I can't run your code, so I can't diagnose your problem for you. The choice is yours:
- provide a fully working example (attach data in a mat file if you need to)
- provide much more details about what you mean by "Doesnt work"
- only provide incomplete descriptions of failure and/or code that cannot be run independently
If you go for option 3 I will not be able to help you. Please let me know your choice, because if it option 3 I know I am wasting my time.
Daniel Ribeiro Gonçalves
on 7 Dec 2019
Answers (1)
Rik
on 25 Nov 2019
The general solution to a problem like this is to put some code around it. The most general solution is to use cell arrays, but if you already know the dimension and type it is a better idea to use an array.
%example of working code:
a=10;b=100;c=30;
some_output=sum(rand(a,b)*c,'all');
some_output2=a*b/c;
array_out_1=cell(10,1);
array_out_2=cell(10,1);
for n=1:numel(array_out)
%first run the working code
a=10;b=100;c=30;
some_output=sum(rand(a,b)*c,'all');
some_output2=a*b/c;
%then store your variables of interest to the cell arrays
array_out_1{n}=some_output;
array_out_2{n}=some_output2;
end
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!