title: Unable to Ensemble average using matlab
body: <p>I have attempted to ensemble average my ecg data(ecg waveform) but I am unable to do so. This code works perfectly for generated cosine waves:</p>
  
  <p>Codes using cosine wave</p>
  
  <pre><code>%test simple ensemble average
  %Generate a signal ’s’ as a sum of two cosine signals with different 
  %frequency using the following command
  n=0:399; %num of samples
  f1=3000;
  f2=5000;
  fs=20*12000; %sample freq
  s1=cos(2*pi*f1*n/fs);
  s2=sin(2*pi*f2*n/fs);
  s=s1+s2; %signal
  figure(2);subplot(4,1,1);plot(n,s); 
  
  %Suppose we have 50 repetitive measurement of the signal ’s’ each added by
  %random noise N(0,sqrt50)
  %These 50 measurements are generated as:
  
  S=repmat(s,50,1);           %50 copies of the signal s
  RN=sqrt(0.5)*randn(50,400); %50 random noise signals of length 400 samples
  MS=S+RN;                    %add random noise to each signal s 
  
  %Show two of the 50 measurements, number 17 and 32
  figure(2);subplot(4,1,2);plot(n,MS(17,:));
  figure(2);subplot(4,1,3);plot(n,MS(32,:));
  
  %Compute the average (ensemble averaging) of the first 9 measurements
  MS9=MS(1:3,:); %select the first 3 column measurements in MS
  MS9
  y9=mean(MS9); %averaged signal
  %show the averaged signal
  figure(2);subplot(4,1,4);plot(n,y9);
  </code></pre>
  
  <p>For my ECG data it returns as "<strong>Index exceeds matrix dimensions</strong>." Which I believe may due to the way I imported my ECG data from csv. </p>
  
  <pre><code>%test code
  clear all;
  clc;
  
  %open data
  fid = fopen('Data.csv');
  %read data frm CSV
  readData = textscan(fid,'%f %f %f','HeaderLines',1,'Delimiter',',');
  fclose(fid);
  %my extract
  x = readData{1,2}(1:1000,1);
  
  %save data in vector form
  test_data=[0,0,0,0]
   for j = 1:length(x)
       test_data(j) = x(j);
   end
  
  fs = 960; %sampling frequency
  N = length(test_data);
  n=0:N; %num of samples
  t = [0:N-1]/fs;
  close all;
  figure (1)
  plot(t,test_data)
  test_data
  
  % Compute the average (ensemble averaging) of the first 10 row measurements
  MS10=test_data(1:10,:); %select the first 10 measurements in MS
  y9=mean(MS10); %averaged signal
  show the averaged signal
  figure(2);subplot(4,1,4);plot(n,y9);
  </code></pre>
  
  <p>As the cosine wave data are stored as :</p>
  
  <p><a href="https://i.stack.imgur.com/XRejA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XRejA.png" alt="enter image description here"></a></p>
  
  <p>But the ECG data is stored as : <a href="https://i.stack.imgur.com/AOmrv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AOmrv.png" alt="enter image description here"></a></p>
  
  <p>Info on Data.csv
  <a href="https://i.stack.imgur.com/jT9HU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jT9HU.png" alt="enter image description here"></a></p>
  
  <p>Output for :</p>
  
  <pre><code>%test_data = readData{2}(1:10000);
  %MS10=test_data(1:10,:);
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/LNRN0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LNRN0.png" alt="enter image description here"></a></p>
  
  <p>Please advise.</p>