Clear Filters
Clear Filters

Index exceeds the number of array elements (9).

5 views (last 30 days)
Ralph
Ralph on 28 Aug 2020
Commented: Ralph on 28 Aug 2020
[row,c]=size(x);
total_frames=row/9;
k=1;
i=5;
aa=rem(row,P+1); % in this case P+1=9;
last_index=row-aa;
for i=5:9:last_index %runs for tatal number of samples in signal
x =[x(i)-1, x(i)-2, x(i)-3, x(i)-4, x(i), x(i)+1, x(i)+2, x(i)+3, x(i)+4];
j=x(i);
I want this code to print the above 9 samples in J variable but it throws an error "Index exceeds the number of array elements (9)".
for r=0:(P/2)-1 % Sumation loop from formula
neighbour=x(i + (r-(P/2)));
center=x(i);
if(neighbour > center+ thresh)
b_left(r+1)=1;
elseif( (neighbour > center-thresh) && (neighbour < center+thresh))
b_left(r+1)=0;
elseif (neighbour < center-thresh)
b_left(r+1)=-1;
end

Answers (1)

KSSV
KSSV on 28 Aug 2020
The error is simple. You are trying to extract more than the number of elements present in the array.
A = rand(5,1) ;
A(1) % no error
A(5) % no error
A(end) % no error
A(6) % error, because there is no sixth element present in A. Only five elements.
  1 Comment
Ralph
Ralph on 28 Aug 2020
Basically my "x" variable contain 9 samples the loop start from 5 and I want to return all elements before and after 5 so can you please tell me how to return these 9 values in variable j.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!