Divide a dataset into subsets

5 ビュー (過去 30 日間)
Greg Athanasiadis
Greg Athanasiadis 2018 年 1 月 31 日
編集済み: Guillaume 2018 年 1 月 31 日
Hello everyone
I have a dataset named chan56 which is 1619936x1 and a vector called markers which is 1x202. The vector markers has the events of the dataset chan56 and i want to split chan56 into subets that contain -1200 indexes from each event until +300. I tried
Data = []
DataMatrix =[zeros(1,length(markers)*1500)];
for n =1 :length(chanPOz)
for i =1:length(markers)
if markers(i) == chanPOz(n)
T = chanPOz(n);
Data = chanPOz(n-1200:n+300);
DataMatrix = [DataMatrix DataMatrix+Data];
end
end
end
But i cant take the DataMatrix
  5 件のコメント
Jos (10584)
Jos (10584) 2018 年 1 月 31 日
I am a little confused by the if statement. Am I right to assume that each value of markers is a direct index into chanPOZ?
Greg Athanasiadis
Greg Athanasiadis 2018 年 1 月 31 日
編集済み: Greg Athanasiadis 2018 年 1 月 31 日
Yes indeed. As an example Assume that i have my data
data = [ 1 5 6 12 15 18 16 12 10 17 25 31 35 36 35 33 32 24 29 35 40 36 33 25]
the vector markers has the indexes of events 18-36-40
markers = [6 14 21]
so i want a subset with [markers(i)-2 markers(i)+1]
In my original dataset i want [markers(i)-1200 markers(i)+300]
first dataset= [-1200th -1199th -1198th ......markers(1).....+299 +300th]
Second dataset= [-1200th -1199th -1198th ......markers(2).....+299 +300th]
...... Last dataset= [-1200th -1199th -1198th ......markers(n).....+299 +300th]

サインインしてコメントする。

採用された回答

Jos (10584)
Jos (10584) 2018 年 1 月 31 日
This is what you're after, I think. No need for a loop over each point in chanPOz
N = numel(markers) ;
% pre-allocate the result:
newData = zeros(N, 1501) ; % 1 extra !! [x-1200:x+300] are 1501 values
% loop over each marker
for n = 1:N
idx = markers(n) ; % the n-th index into chanPOz
newData(n,:) = chanPOz(idx-1200:idx+300); % get the data and store this in the n-th row
end
  1 件のコメント
Greg Athanasiadis
Greg Athanasiadis 2018 年 1 月 31 日
編集済み: Greg Athanasiadis 2018 年 1 月 31 日
Thank you both for your answers!!!!
I ran Jos solution because i have Matlab 2015b but i had problems with the limits so i add an if statement so the loop started from 1200 and ended on N-300. Matlab ran the loop but the matrix new Data was still with zeros

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2018 年 1 月 31 日
Even a single loop is not needed. A double loop is a complete waste of time.
Assuming R2016b or later,
assert(all(markers > 1200 & markers < numel(chanPoz) - 300), 'some markers too close to the edges');
indices_to_extract = markers(:) + (-1200:300); %create a matrix of indices
extracteddata = chanPoz(indices_to_extract.'); %extract relevant data as a matrix
Each column of extracteddata is the -1200 to 300 points around the corresponding marker. If you do want it as a vector (but why?) then you can then do:
extracteddata = reshape(extracteddata, 1, []);
If you're on a version earlier than R2016b, then the indices_to_extract line becomes
indices_to_extract = bsxfun(@plus, markers(:), -1200:300);
  3 件のコメント
Greg Athanasiadis
Greg Athanasiadis 2018 年 1 月 31 日
I got Error using + Matrix dimensions must agree.
Guillaume
Guillaume 2018 年 1 月 31 日
編集済み: Guillaume 2018 年 1 月 31 日
I clearly wrote:
If you're on a version earlier than R2016b, then the indices_to_extract line becomes
indices_to_extract = bsxfun(@plus, markers(:), -1200:300);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by