Fill array with values of other array from index1 to index2

3 visualizaciones (últimos 30 días)
Christine Abou Nasr
Christine Abou Nasr el 30 de Mayo de 2017
Editada: Jan el 30 de Mayo de 2017
Hello! I have one array (one double column) DateN which contains 21000 values that represent 6 months of dates and time. However I would like to have another array (one double column) that would start at a specific date and end on another (index1 and index2). I tried this code but it freezes matlab and have to restart it (i understand why now)
v=index2-(index1-1);
for g1=1:v
for g2=index1:(index2-1)
global Temps
Temps(g1,1)= DateN(g2,1);
g1=g1+1;
g2=g2+2;
end
end
and I tried this code:
g2=1;
for g1=index1:index2
Temps(g2,1)= DateN(g1,1);
g1=g1+1;
g2=g2+1;
end
But instead of getting index2-index1 values in Temps I get 3000 more values. Can anyone help? I am using R2013a. Thank you very much!

Respuesta aceptada

Jan
Jan el 30 de Mayo de 2017
Editada: Jan el 30 de Mayo de 2017
Do you mean:
Temps = DateN(index1:index2, 1);
Using global 's is surely a bad idea. Avoid globals strictly to allow for an efficient debugging.
for g1=index1:index2
...
g1=g1+1;
end
You should get a warning in the editor: Modifying the loop counter inside the loop is meaningless. g1 is incremented by the FOR loop already.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by