Table and Array Indexing using loops

Hi everyone,
I have a challenge in indexing in a for loop.
I have a 696 by 2 table with a date identifier. This is as captured below. I would like to create a [648 4] from the [696 2] using the date identifier or otherwise.
I would like to create a new table such that
i=24;
n=numel(24:1:648);
H=zeros(n, 3);
for N=1:n
for i=24:648
i=[(i-days(1):days(1):i+days(2))]
% yields for me a new matrix for i=[GHI([i-day(1) i i+day(1)]
% which then gives me [648 3]
i=i+1;
end
H(N,:)=table2array(hvalues(i,2:4));
end
Any help on the above is highly appreciated.

9 comentarios

dpb
dpb el 23 de Mzo. de 2019
The indexing expression is simply
newdata(i-24,:)=data([i-24 i i+24],2);
Guillaume
Guillaume el 23 de Mzo. de 2019
Rather than giving us code that makes no sense at all, why don't you explain what it is you want.
Image Analyst
Image Analyst el 23 de Mzo. de 2019
and attach a mat file with the table in it so we can code up something with your actual data. This will save you time in getting an answer from us.
Your table2array() call is using i after the end of the for i loop. The for loop would leave i with the last value it was set to during the loop. Each iteration of for i, any changes that were made to i during previous iterations are ignored and i will be set to the next value in sequence. The operation is like
hidden_i_start = 24;
hidden_i_increment = 1;
hidden_i_end = 648;
hidden_i = hidden_i_start;
if hidden_i_start > hidden_i_end
i = [];
else
while hidden_i <= hidden_i_end
i = hidden_i;
i = i-days(1):days(1):i+days(2);
i = i + 1;
hidden_i = hidden_i + hidden_i_increment;
end
end
Observe that no matter what change you make to i in the loop, the next iteration i will get changed back to the next number in the sequence established by the initial for specification, but that on the last iteration, i will not be changed. So at the end of the for i loop with you changing i in that way, you will end up with i being set to (648-days(1)):days(1):648+days(2)) + 1
and then after that loop, you will use that final i as the row index into hvalues. Are you sure that is a good idea?
Lui
Lui el 24 de Mzo. de 2019
Given the table in the attached file create a new table with two more columns with data such that:
% row_date(i) = ([GHI((i)-days(1)) GHI(i) (GHI(i)+days(1))])
for example from the attached file
% values for (2004 02 02 10 0 0) = [137.42 118.9442 201.7317].
We initialize at i=25 to give for the one day allowance.
The aim is to increase the sample size of my data by sampling the data at 24 hours intervals.
Thank you.
Walter Roberson
Walter Roberson el 24 de Mzo. de 2019
Editada: Walter Roberson el 24 de Mzo. de 2019
Lui comments to dpb:
This works. I appreciate your help.
Rik
Rik el 7 de Mayo de 2019
Editada: Walter Roberson el 7 de Mayo de 2019
Comment posted as flag by Lui:
Would you please let me know why this does not work when I want to create n columns instead of 3.
data =table2array(data(:,2));
newdata(i-48, :)=data([i-48 i-24 i i+24 i+48], 1)
it returns an error. What could be the challenge? @dpb
dpb
dpb el 7 de Mayo de 2019
What's the specific error text (all the red from the command line in context)???
I'd have to wrap my head around the original Q? again to remind myself but there's nothing specifically wrong with the expression other than one has to ensure that i-48 is >=1 and newdata hasn't been previously defined as a noncormant size that this vector won't fit into...but functionally, it's the same idea, yes, and "it will work" given the constraints of where it is attempted are legitimate.
Walter Roberson
Walter Roberson el 7 de Mayo de 2019
We do not know which version of the code was being used, so we do not really have any useable context for thinking there is an error.

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 24 de Mzo. de 2019
The aim is to increase the sample size of my data by sampling the data at 24 hours intervals.
why didn't you say that in the first place? That's a typical case of XY problem. You want to do something, don't know how to do it but think it can be done one way so ask about that way. Except that's not how it's done.
The simplest way to resample timed data is to convert your table into a timetable then use retime to resample it to whichever interval you want
hvalues = table2timetable(hvalues);
newvalues = retime(hvalues, 'daily', 'linear'); %resample at 24 hours interval using linear interpolation
However, I'm a bit confused, your data is hourly so I'm not sure how resampling in 24 hours interval is going to increase the sample size.

1 comentario

Lui
Lui el 25 de Mzo. de 2019
This gives me hourly values which I can easily retrieve. But I appreciate as this helps me elsewhere.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2018a

Preguntada:

Lui
el 23 de Mzo. de 2019

Comentada:

el 7 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by