Code only works in the command window
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Guilherme Lopes de Campos
il 8 Feb 2019
Commentato: Guilherme Lopes de Campos
il 12 Feb 2019
Hi MATLAB community,
I am trying to execute the follow code, but this only works in the command window and not editor.
Could you help me?
Guilherme Lopes de Campos
% dados is a matriz 13817 rows and 3 columns
function [matriz_medias] = gerar_medias(dados)
[linha,coluna]=size(dados);
matriz(1:linha,1:4)=zeros;
matriz(1:linha,2:4)=dados;
j=1;
k=1;
ano_ini=matriz(1,3);
for i=k:linha
if matriz(k,3)==ano_ini
matriz(k,1)=j;
k=k+1;
while matriz(k,3)==ano_ini
matriz(k,1)=j;
k=k+1;
end
while matriz(k,3)>ano_ini
matriz(k,1)=j;
k=k+1;
end
end
j=j+1;
end
matriz_media=zeros(((matriz(linha,3)-matriz(1,3))+1)*12,(matriz(linha,1)+2));
instrumento=1;
ano=matriz(1,3);
mes=1;
for i=1:((matriz(linha,3)-matriz(1,3))+1)*12
if rem(i,12)~=0
matriz_media(i,1)=ano;
matriz_media(i,2)=mes;
mes=mes+1;
else
matriz_media(i,1)=ano;
matriz_media(i,2)=mes;
mes=1;
ano=ano+1;
end
end
mes=1;
n=0;
j=1;
soma=0;
instrumento=1;
diferenca=0;
for i=1:linha
if matriz(i,2)==mes
soma=soma+matriz(i,4);
n=n+1;
else
matriz_media(j,instrumento+2)=soma/n;
soma=0;
n=0;
diferenca=matriz(i,2)-mes;
if diferenca>1
for k=1:(diferenca-1)
j=j+1;
matriz_media(j,instrumento+2)=999999;
mes=mes+1;
end
diferenca=0;
end
soma=matriz(i,4);
n=1;
if mes<12
mes=mes+1;
else
mes=1;
end
if matriz(i,1)~=instrumento
instrumento=instrumento+1;
j=j+1;
end
end
end
end
2 Commenti
OCDER
il 8 Feb 2019
How are you executing your code in the command window and what do you mean it runs in the editor? Are you getting an error message? If so, copy paste the full error message.
Risposta accettata
OCDER
il 8 Feb 2019
Looks like your while loop counter index, k, is going above the row count of matriz. For instance, check these simpler codes out to replicate your error and to fix it.
matriz = ones(1000, 3);
ano_ini = 1;
k = 1;
j = -1;
while matriz(k,3)==ano_ini %Error: Index exceeds matrix dimensions
matriz(k,1)=j;
k=k+1;
end
matriz = ones(1000, 3);
ano_ini = 1;
k = 1;
j = -1;
while matriz(k,3)==ano_ini
matriz(k,1)=j;
k=k+1;
if k > size(matriz, 1); break; end %break while loop before an error occurs
end
5 Commenti
Jan
il 12 Feb 2019
Without meaningful comments it is hard to guess, what the code should do.
j = 1;
k = 1;
ano_ini = 1;
for i = k:linha
if matriz(k,3) == ano_ini
matriz(k,1) = j;
k = k+1;
while matriz(k,3) == ano_ini
matriz(k, 1) = j;
k = k + 1;
if k > size(matriz, 1);
break;
end
end
while k <= size(matriz, 1) && matriz(k, 3) > ano_ini
end
end
j = j+1;
end
The 2nd while loop is either not entered at all or runs infinetly. So you can simply omit it.
The 1st while loop fills the first column of matriz with ones, but the elements are 1 before also. I have no idea, what the prupose of this code is. I guess, that you do not need a loop at all.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Conway's Game of Life in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!