for loop through matrix index

35 次查看(过去 30 天)
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 个评论
David Hill
David Hill 2022-10-28
Is simple example of inputs and expected output would be helpful. I am not understanding your description.
Enzo
Enzo 2022-10-28
Hi @David Hill you are very helpfull, as you are replying to all of my inquieries, so thanks so much. Anyway, as the actual data is too big to show you the actual preferred outcome, try to run
S = new_ch((lc-pre_stim):(lc+(post_stim-1)),:);
S
and comment the final for loop.
S is one of my expected result. The for loop should be able to create 129 variables (each of them will contain a 2001x33 matrix with actual values extracted from new_ch table), and place them into a 3D matrix. Otherwise, if the 3D matrix is too work for you to come up with, I will be very happy to have the 129 "S" variables stored and then I will create a 3D matrix myself

请先登录,再进行评论。

采纳的回答

David Hill
David Hill 2022-10-28
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 个评论
David Hill
David Hill 2022-10-28
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?
Enzo
Enzo 2022-10-28
@David Hill fair point. By the way, i have got full control over the time the trigger will be delivered. This mean, I know the values will never fall under or atop of the matrix index. Thus, all the matrices will always be of the same lenghts (rows x columns), therefore no worries.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by