# fix the indices of the matrix

3 views (last 30 days)
Tu Nguyen on 21 Mar 2022
Commented: Tu Nguyen on 21 Mar 2022
I really need help to fix the indices of the for loop to run the QRS detection.
I updated the file, this code run with the file ech_60hz_200 that I downl;oad from bio bank, but it cannot run with the code ecg_sbj1 that I recorded in the lab room. it returned 'Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.' Please help me fix this indices in the for loop to run the code
close all;clear;clc;
load('ecg_sbj1.mat');
sig=ecg;
N=length(sig);
fs=200;
t=[0:N-1]/fs;
title('Original Signal')
b=1/32*[1 0 0 0 0 0 -2 0 0 0 0 0 1];
a=[1 -2 1];
sigL=filter(b,a,sig);
b=[-1/32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1/32];
a=[1 -1];
sigH=filter(b,a,sigL);
b=[1/4 1/8 0 -1/8 -1/4];
a=[1];
sigD=filter(b,a,sigH);
sigD2=sigD.^2;
signorm=sigD2/max(abs(sigD2));
h=ones(1,31)/31;
sigAV=conv(signorm,h);
sigAV=sigAV(15+[1:N]);
sigAV=sigAV/max(abs(sigAV));
treshold=mean(sigAV);
P_G= (sigAV>0.01);
difsig=diff(P_G);
left=find(difsig==1);
raight=find(difsig==-1);
left=left-(6+16);
raight=raight-(6+16);
for i=1:length(left);
[R_A(i) R_t(i)]=max(sigL(left(i):raight(i)));
R_t(i)=R_t(i)-1+left(i) %add offset
[Q_A(i) Q_t(i)]=min(sigL(left(i):R_t(i)));
Q_t(i)=Q_t(i)-1+left(i)
[S_A(i) S_t(i)]=min(sigL(left(i):raight(i)));
S_t(i)=S_t(i)-1+left(i)
[P_A(i) P_t(i)]=max(sigL(left(i):Q_t(i)));
P_t(i)=P_t(i)-1+left(i)
[T_A(i) T_t(i)]=max(sigL(S_t(i):raight(i)));
T_t(i)=T_t(i)-1+left(i)+47
end
figure;plot(t,sigL,t(Q_t),Q_A,'*g',t(S_t),S_A,'^k',t(R_t),R_A,'ob',t(P_t),P_A,'+b',t(T_t),T_A,'+r');
for i=1:((length(P_t))-1)
HRV=P_t(i+1)-P_t(i)
end
##### 2 CommentsShow NoneHide None
Davide Masiello on 21 Mar 2022
Please give more info about what is there to fix.
Tu Nguyen on 21 Mar 2022
The problem is in the for loop, it return 'Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.'. I dont know why

Sign in to comment.

### Answers (2)

Walter Roberson on 21 Mar 2022
In each case your left values are greater than your corresponding right values, so left(i):right(i) is always empty.
##### 2 CommentsShow NoneHide None
Tu Nguyen on 21 Mar 2022
The problem is when I load the raw data ecg grom biobank, size 476x1, the code is running, but when I load the ecg data that I recorded in the lab room with size 12000x1, it does not run. I dont know the error said: 'Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.'
Please help me fix that error
Walter Roberson on 21 Mar 2022
Immediately before your
for i=1:length(left);
statement, put in
disp([left, right])
You will see that in every case, the value in the left column is greater than the value in the right column. You use those values as the start and stop indices left(i):right(i) but with left being greater than right, that is empty. Computing with empty gives you empty, so the right hand side of the computation is empty. You are trying to store that emptiness into a scalar location, which is a size mismatch.
You are assuming that difsig==1 always occurs before difsig==-1 but what if that is not the case?? What if the signal zags first instead of zigging first?

Sign in to comment.

VBBV on 21 Mar 2022
close all;clear;clc;
load('ecg_sbj1.mat')
sig=ecg
sig = 12000×1
31 30 32 32 31 30 31 32 31 30
N=length(sig);
fs=200;
t=[0:N-1]/fs
t = 1×12000
0 0.0050 0.0100 0.0150 0.0200 0.0250 0.0300 0.0350 0.0400 0.0450 0.0500 0.0550 0.0600 0.0650 0.0700 0.0750 0.0800 0.0850 0.0900 0.0950 0.1000 0.1050 0.1100 0.1150 0.1200 0.1250 0.1300 0.1350 0.1400 0.1450
b=1/32*[1 0 0 0 0 0 -2 0 0 0 0 0 1];
a=[1 -2 1];
sigL=filter(b,a,sig)
sigL = 12000×1
0.9688 2.8750 5.7812 9.6875 14.5625 20.3750 25.2188 29.1875 32.1250 34.0000
b=[-1/32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1/32]
b = 1×33
-0.0312 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 -1.0000 0 0 0 0 0 0 0 0 0 0 0 0
a=[1 -1];
sigH=filter(b,a,sigL);
b=[1/4 1/8 0 -1/8 -1/4];
a=[1];
sigD=filter(b,a,sigH)
sigD = 12000×1
-0.0076 -0.0338 -0.0902 -0.1847 -0.3175 -0.4885 -0.6821 -0.8761 -1.0477 -1.1812
sigD2=sigD.^2;
signorm=sigD2/max(abs(sigD2));
h=ones(1,31)/31;
sigAV=conv(signorm,h);
sigAV=sigAV(15+[1:N]);
sigAV=sigAV/max(abs(sigAV));
treshold=mean(sigAV);
P_G= (sigAV>0.01);
difsig=diff(P_G);
left=find(difsig==1);
raight=find(difsig==-1);
left=left-(6+16)
left = 148×1
107 116 172 257 328 410 490 577 655 741
raight=raight-(6+16)
raight = 148×1
94 112 129 246 281 401 439 564 593 729
max(left)
ans = 11929
max(raight)
ans = 11871
min(left)
ans = 107
min(raight)
ans = 94
for i=1:length(left);
if raight(i) < left(i)
[R_A(i) R_t(i)]=max(sigL(raight(i):left(i)));
R_t(i)=R_t(i)-1+left(i); %add offset
else
[R_A(i) R_t(i)]=max(sigL(left(i):raight(i)));
R_t(i)=R_t(i)-1+left(i); %add offset
end
if left(i) < R_t(i)
[Q_A(i) Q_t(i)]=min(sigL(left(i):R_t(i)));
Q_t(i)=Q_t(i)-1+left(i);
else
[Q_A(i) Q_t(i)]=min(sigL(R_t(i):left(i)));
Q_t(i)=Q_t(i)-1+left(i);
end
if raight(i) < left(i)
[S_A(i) S_t(i)]=min(sigL(raight(i):left(i)));
S_t(i)=S_t(i)-1+left(i);
else
[S_A(i) S_t(i)]=min(sigL(left(i):raight(i)));
S_t(i)=S_t(i)-1+left(i);
end
if left(i) < Q_t(i)
[P_A(i) P_t(i)]=max(sigL(left(i):Q_t(i)));
P_t(i)=P_t(i)-1+left(i);
else
[P_A(i) P_t(i)]=max(sigL(Q_t(i):left(i)));
P_t(i)=P_t(i)-1+left(i);
end
if S_t(i) < raight(i)
[T_A(i) T_t(i)]=max(sigL(S_t(i):raight(i)));
T_t(i)=T_t(i)-1+left(i)+47;
else
[T_A(i) T_t(i)]=max(sigL(raight(i):S_t(i)));
T_t(i)=T_t(i)-1+left(i)+47;
end
end
% compare two plots differently
subplot(211)
plot(t(1:length(Q_t)),Q_A,'*g',t(1:length(S_t)),S_A,'^k',t(1:length(R_t)),R_A,'ob',t(1:length(P_t)),P_A,'+b',t(1:length(T_t)),T_A,'+r');
subplot(212)
plot(t,sigL)
% title('Original Signal')
for i=1:((length(P_t))-1)
HRV=P_t(i+1)-P_t(i);
end
You need to use a condition to segregate data in lower to higher order in data arrays for which you add offsets .
##### 1 CommentShow -1 older commentsHide -1 older comments
Tu Nguyen on 21 Mar 2022
Can you help me? I want the QRS on the signal sigL, I plot but it showed 'Index exceeds the number of array elements (12000).'

Sign in to comment.

### Categories

Find more on Measurements and Feature Extraction in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!