How to approach iterative data reading?

I'm working with a dataset that has 3 different arrays I'm interested in plotting: time, accel, and gcamp. They are all of equal size.
I'm using the accel variable as a threshold detector, where I define an arbitrary threshold under which any data I ignore. Anything above that threshold I use the index of that data point to grab the equivalent data points from time and gcamp.
How can I store this data in one structure so that I can iterate through it and generate statistics/plots about only the thresholded data points?
Something like:
time = [rand(1, 5000)];
accel = [rand(1,5000)];
gcamp = [rand(1,5000)];
sd = std(accel);
threshold = sd;
dumb = [];
for i = 1:length(accel);
if accel(i) > threshold;
dumber = [time(i), gcamp(i)];
dumb = [dumb, dumber];
end
end

 Accepted Answer

Hi Eric,
Use logical indexing.
time = [rand(1, 5000)];
accel = [rand(1,5000)];
gcamp = [rand(1,5000)];
sd = std(accel);
threshold = sd;
dumb = [];
for i = 1:length(accel);
if accel(i) > threshold;
dumber = [time(i), gcamp(i)];
% dumb = [dumb, dumber];
dumb = [dumb; dumber];
end
end
index = accel > threshold;
foo = [time(index);gcamp(index)].'; % transpose to match dumb
isequal(foo,dumb)
ans = logical
1

More Answers (0)

Categories

Asked:

on 3 Apr 2026 at 0:10

Answered:

on 3 Apr 2026 at 2:00

Community Treasure Hunt

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

Start Hunting!