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
回答(2 个)
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)
I am not certain what you want to do, however the logical vector approach (the 'Lv' variable here) is probably more efficient that the explicit loop.
One option to segregate the values after threshoding them is to find the beginning and end locations of each series of 1 values in Lv and use that to segment the signal. You can do whatever you want with the segments after that.
Try something like this --
% time = [rand(1, 5000)];
time = 0:4999;
accel = sin(time*2*pi/500);
gcamp = cos(time*2*pi/400);
sd = std(accel);
threshold = sd;
Lv = accel > threshold;
Valid = nnz(Lv)
timev = time(Lv);
gcampv = gcamp(Lv);
% Lv
startv = strfind(Lv, [0 1]) + 1;
stopv = strfind(Lv, [1 0]);
if startv(1) > stopv(1)
startv = [1 startv];
end
if numel(startv) > numel(stopv)
stopv = [stopv numel(Lv)];
end
for k = 1:numel(startv)
idxrng = startv(k):stopv(k);
ixr{k} = idxrng([1 end]);
segment{k} = [time(idxrng); gcamp(idxrng)];
end
ixr{1}
segment{1}
ixr{2}
segment{2}
ixr{end}
segment{end}
figure
hold on
for k = 1:numel(segment)
plot(segment{k}(1,:), segment{k}(2,:))
end
hold off
grid
showMissing = zeros(size(time)) - 0.2;
showMissing(Lv) = 0.2 * ones(1,Valid);
figure
plot(time, gcamp)
hold on
plot(time, showMissing, 'r')
hold off
grid
figure
stairs(time, gcamp, '-')
hold on
stairs(time, showMissing, '-r')
hold off
grid
xlim([1000 1900])
ylim([-1.1 1.1])
% dumb = [];
% for i = 1:length(accel);
% if accel(i) > threshold;
% dumber = [time(i), gcamp(i)];
% dumb = [dumb, dumber];
% end
% end
.
类别
在 帮助中心 和 File Exchange 中查找有关 MAB Modeling Guidelines 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


