Code only works in the command window
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Guilherme Lopes de Campos
le 8 Fév 2019
Commenté : Guilherme Lopes de Campos
le 12 Fév 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 commentaires
Réponse acceptée
OCDER
le 8 Fév 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!