Storage of variables being generated in a loop

조회 수: 2 (최근 30일)
Daniel Ribeiro Gonçalves
Daniel Ribeiro Gonçalves 2019년 11월 24일
댓글: Daniel Ribeiro Gonçalves 2019년 12월 7일
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
Rik
Rik 2019년 12월 4일
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:
  1. provide a fully working example (attach data in a mat file if you need to)
  2. provide much more details about what you mean by "Doesnt work"
  3. 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
Daniel Ribeiro Gonçalves 2019년 12월 7일
Thank you a lot for your time Rik! I got it by other way. The way you were sugesting would'nt work cuz my while loop would stop every time it reached the sample(horas) I seted.
So I just raised my time of iteration in my main while loop, and divided my indexes vector to the new sample I wanted to set(ask for input), and also created a for only to build my indexes..

댓글을 달려면 로그인하십시오.

답변 (1개)

Rik
Rik 2019년 11월 25일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by