Storage of variables being generated in a loop

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

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.
I wanna save the indexes saifi, saidi and asai, which depend on TDT7, and contP7, which will be given, when my algorithm converge. I want my code to be run 10 times so I can have 10 different of my indexes, so I need to store then to have a mean in the following
Thanks
The code you posted has syntax issues, so it is impossible to run your code. There are also at least some variables missing.
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
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.
This code that I sent here is running. I did what you wrote to me now, but I need to run my while loop to be run 50 times, and I cant make that happen.
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?
cargas=[6 80 30 105 75 90 54.5; %Y-PQ
8 0 0 10 5.8 0 0; %D-Z
9 0 0 0 0 2.5 1.45; %Y-I
10 275 174.5 237 130 230 120; %D-PQ
12 5 2 0 0 10 6; %Y-Z
13 5 3 0 0 0 0; %Y-Z
15 0 0 20 13 9.5 4.6;%Y-I
16 0 0 3.5 2 0 0]; %Y-PQ
itermax=20;
Sbase=100;
Va=1 + 0i;
Vb=1 + 0i;
Vc=1 + 0i;
ea=real(Va);
fa=imag(Va);
eb=real(Vb);
fb=imag(Vb);
ec=real(Vc);
fc=imag(Vc);
n=(cargas(:,1))';
Pda=cargas(:,2);
Pdb=cargas(:,4);
Pdc=cargas(:,6);
Qda=cargas(:,3);
Qdb=cargas(:,5);
Qdc=cargas(:,7);
Sa=[Pda+Qda*j]/Sbase;
Sb=[Pdb+Qdb*j]/Sbase;
Sc=[Pdc+Qdc*j]/Sbase;
Stot=Pda+Pdb+Pdc;
tempos= [1 2 4 0.5 4 0.678928 4 0.678928 4 0.678928 4 0.678928 4 0.678928 4 0.678928 0 sum(Stot) 2152; %01-02
2 2 7 0.5 7 0.688064 5 0.688064 5 0.688064 5 0.688064 5 0.688064 5 0.688064 0 Stot(2)+Stot(3) 45; % 02-07
3 3 7 0.5 7 0.688064 5 0.688064 5 0.688064 5 0.688064 5 0.688064 5 0.688064 40 Stot(2) 40; % 07-08
4 6 7 0.5 7 0.688064 10 0.688064 10 0.688064 10 0.688064 10 0.688064 10 0.688064 5 Stot(3) 5; % 07-09
5 3 4 0.5 4 0.678928 4 0.678928 4 0.678928 4 0.678928 4 0.678928 4 0.678928 0 sum(Stot(1)+Stot(4:8)) 2097; % 02-03
6 5 4 0.5 4 0.678928 4 0.678928 4 0.678928 2 0.678928 4 0.678928 4 0.678928 1500 Stot(4) 1500 ; % 03-10
7 10 4 0.5 4 0.678928 4 0.678928 2 0.678928 2 0.678928 4 0.678928 4 0.678928 0 sum(Stot(1)+Stot(5:8)) 597; %03-04
8 2.5 7 0.5 7 0.688064 6 0.688064 6 0.688064 6 0.688064 6 0.688064 6 0.688064 0 sum(Stot(5:6)) 40; % 04-11
9 2 7 0.5 7 0.688064 6 0.688064 6 0.688064 6 0.688064 6 0.688064 6 0.688064 30 Stot(5) 30; % 11-12
10 3 7 0.5 7 0.688064 11 0.688064 11 0.688064 11 0.688064 11 0.688064 11 0.688064 10 Stot(6) 10; % 11-13
11 3 4 0.5 4 0.678928 4 0.678928 2 0.678928 2 0.678928 2 0.678928 2 0.678928 0 sum(Stot(1)+Stot(7:8)) 567; % 04-05
12 1.5 7 0.5 7 0.688064 6 0.688064 6 0.688064 6 0.688064 6 0.688064 6 0.688064 0 sum(Stot(7:8)) 67; % 05-14
13 3 7 0.5 7 0.688064 6 0.688064 6 0.688064 6 0.688064 6 0.688064 6 0.688064 60 Stot(7) 60;% 14-15
14 5 7 0.5 7 0.688064 11 0.688064 11 0.688064 11 0.688064 11 0.688064 11 0.688064 7 Stot(8) 7; % 14-16
15 2 4 0.5 4 0.678928 4 0.678928 2 0.678928 2 0.678928 2 0.678928 2 0.678928 500 Stot(1) 500] ;% 05-06
% A penúltima coluna representa a potencia de cada trecho de linha,
% isto é, a potência que deixará de ser entregue caso ocorra uma falha
% naquele trecho
%A penultima linha representa o número de consumidores que são
%atendidos por aquele trecho de linha
I have no clue what you mean by this block of unformatted code.
This is the data I'm calling when I run my code.
The issue I'm having is I still the same. I can't store my indices for 50 while loops in one matrix that should be 50x7 in this case
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];
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.
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..

Sign in to comment.

Answers (1)

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!