For loop optimization help for working with large data

I am working with a data set collected from a wearable wristband that tracks phsyiological data and has a button that creates a data point with the posix time when it was pressed. We are having participants indicate increases by pressing the button two times consecutively and one standalone press indicates a decrease. Now I am trying to perform a vector that goes through each time table and assign a running total that is synchronized to the data collection times (ex. if an increase event occurs at 8AM everything before then should be whatever the previous value. At 8AM the running total would increase by 1 and carry that total until adjusted by the next tagged event and so on and so forth)
Right now I am using a for loop that goes through each row and sees if the time of each physiological data point is between each tagged time point and then I assign that a value based on the already calculated running total variable.
y= numel(TEMPX);
TEMPtry = randi(10,y,1);
for l = 1:numel(TEMPX)
if TEMPX.Time(l) <= Tagstime(1,1)
TEMPtry(l,1)= 0;
continue
end
if TEMPX.Time(l) > Tagstime(1,1) && TEMPX.Time(l) < Tagstime(2,1)
TEMPtry(l,1) = total(1,1);
continue
end
if TEMPX.Time(l) >= Tagstime(2,1) && TEMPX.Time(l) < Tagstime(3,1)
TEMPtry(l,1) = total(2,1);
continue
end
if TEMPX.Time(l) >= Tagstime(3,1) && TEMPX.Time(l) < Tagstime(4,1)
TEMPtry(l,1) = total(3,1);
continue
end
if TEMPX.Time(l) >= Tagstime(4,1) && TEMPX.Time(l) < Tagstime(5,1)
TEMPtry(l,1) = total(4,1);
continue
end
if TEMPX.Time(l) >= Tagstime(5,1) && TEMPX.Time(l) < Tagstime(6,1)
TEMPtry(l,1) = total(5,1);
continue
end
if TEMPX.Time(l) >= Tagstime(6,1) && TEMPX.Time(l) < Tagstime(7,1)
TEMPtry(l,1) = total(6,1);
continue
end
if TEMPX.Time(l) >= Tagstime(7,1) && TEMPX.Time(l) < Tagstime(8,1)
TEMPtry(l,1) = total(7,1);
continue
end
if TEMPX.Time(l) >= Tagstime(8,1) && TEMPX.Time(l) < Tagstime(9,1)
TEMPtry(l,1) = total(8,1);
continue
end
if TEMPX.Time(l) >= Tagstime(9,1) && TEMPX.Time(l) < Tagstime(10,1)
TEMPtry(l,1) = total(9,1);
continue
end
if TEMPX.Time(l) >= Tagstime(10,1)
TEMPtry(l,1) = total(10,1);
continue
end
end
beep on
*Note: this is from a sample data set I was working from where there only ended up that having 10 tagged items. In an actual data set the number of tagged items could vary with no defined maximum. "TEMPX" is a timetable, "Tagstime" is a datetime variable, "total" is a double
As you could imagine this script takes FOREVER to complete when y = 342056 since the sample rate is so high and I am currently using a "for loop" method. Can someone help me come up with a better way to achieve this operation? I looked at some other posts that recommended vectorization but I am not sure how to apply that method to achieved the desired outcome.
desired output: Tags [8:00:01; 8:00:02; 12:00:01; 13:01:01; 13:01:02], total [1,0,1],
TEMPtry(starttime: 8:00:00, 1) = 0
TEMPtry(8:00:01:12:00:00, 1) = 1
TEMPtry(12:00:01:13:00:00, 1) = 0
TEMPtry(13:00:01: end, 1) = 1
*I know that's not the correct notation but hopefully that gives you an idea of what I'm trying to do.

4 comentarios

Need more information. Can you post a small portion of your data file?
Cameron Johnson
Cameron Johnson el 28 de En. de 2022
Editada: Cameron Johnson el 28 de En. de 2022
These are a few sample files that I created. Does this help?
*updated with complete workspace
Yes, but it would help us more if you put all of your variables in a single .mat file instead of 3 separate ones. That way there is less for us to download/load.
Thanks for the suggestion. That makes way more sense to do it like this

Iniciar sesión para comentar.

Respuestas (1)

Matt J
Matt J el 28 de En. de 2022
Editada: Matt J el 29 de En. de 2022
load('Example Workspace.mat')
edges=[seconds( Tagstime-Tagstime(1) );inf];
T=seconds( TEMPX.Time-Tagstime(1) );
bin=discretize(T , edges);
inside=~isnan(bin);
TEMPtry = zeros(numel(TEMPX),1);
TEMPtry(inside)=total(bin(inside));
plot(TEMPtry); axis padded

7 comentarios

Cameron Johnson
Cameron Johnson el 28 de En. de 2022
Editada: Cameron Johnson el 28 de En. de 2022
I tried this solution but it only returned zeros. Could you elaborate a bit
I made a small modification and ran it with your posted data. Now there are non-zeros.
Thank you thats a lot closer to what I need but I just need it to hold that last number through out the rest of the data sample but right now after it runs through the tags it immediately goes back to zero.
I'll send over some replica data that you can work with.
Make sure you have my latest version.
That's perfect. I really appreciate it
Cameron Johnson
Cameron Johnson el 29 de En. de 2022
Editada: Cameron Johnson el 2 de Feb. de 2022
Actually there does seem to be one small hiccup. It only seems to work for the pairs where there is a second between them (indicating an increase) but it doesn't work well with the single button presses (indicating a decrease). If you could make some suggestions that would be extremely appreciated.
I suggest you read the documentation for discretize(),
My initial answer chose the bin edges with Tagstime rounded to seconds,
edges=[seconds( Tagstime-Tagstime(1) );inf];
but there is nothing sacred about that choice.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2021b

Preguntada:

el 28 de En. de 2022

Comentada:

el 3 de Feb. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by