for loop through matrix index
34 views (last 30 days)
Show older comments
Hello everyone,
a very basic issue which is causing me a lot of pain (drama queen mode ON).
How to iterate on operation using a matrix index, currently stored in variable "lc" (which has all the rows index I want to use) and I would like to use these rows index in the new_ch variable.
In practice, I would like to use the index stored in "lc" as a index localization for the variables stored in the rows (the rows from 1 to 32) in "new_ch" variable.
using these lines of code (which you will find in the "big" code below), I obtain in variable S exactly what I am looking for, but it only work out one time (of course)
% S = new_ch((lc-pre_stim):(lc+(post_stim-1)),:);
% S
The final loop is my attempt, which is not working. the error code:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in extract_threshold_ADC (line 32)
V = i((new_ch-pre_stim):(new_ch+(post_stim-1)),:);
if you are able to go through all this process without using the for loop feel free to make any suggestion.
If needed, please download the file following these link: actual data being used
Thanks so much in advance, any tips will be very appreciated
adc_chan = 3; % to be set according to the ADC channels being used
load Open_Ephys_2022-10-19_14-10-17_st1.mat ADC CH;
new_ch = horzcat(CH, ADC(:,adc_chan));
%% Step 2: define a threshold for channel 35 (ADC channel) values and retain all the rows from
thr = 4; % determine the minimun threshold amplitude for stimulation being applied
idx = new_ch(:,33)>thr;
values_to_keep = new_ch(idx,:);
%% find peak in column 33 (only spot the rising edge. see lc to index location)
fs = 10000; % sampling frequency
[pk,lc] = findpeaks(new_ch(:,33), MinPeakHeight=thr);
%% define epoch features and extract epochs
pre_stim = 5000; % period pre-stimulus to retain
post_stim = 15000; % period post-stimulus to retain
% S = new_ch((lc-pre_stim):(lc+(post_stim-1)),:);
% S
for i = 1:length(lc)
V = i((new_ch-pre_stim):(new_ch+(post_stim-1)),:);
end
4 Comments
David Hill
on 28 Oct 2022
Is simple example of inputs and expected output would be helpful. I am not understanding your description.
Accepted Answer
David Hill
on 28 Oct 2022
Your sizes can be different, store in cell array.
for i = 1:length(lc)
S{i} = new_ch(max([lc-pre_stim,1]):min([lc+(post_stim-1),size(new_ch,1)]),:);
end
3 Comments
David Hill
on 28 Oct 2022
lc-pre_stim == lc - 5000, If any lc <5001 then your are going to have problems.
lc+post_stem-1 == lc + 14999, If any lc > size(new_ch,1) -14999 then you are also going to have problems.
How can you ensure lc index is not going to be in the top or bottom of new_ch?
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!