How to deal with Index exceeds the number of array elements ?

조회 수: 1(최근 30일)
Tomaszzz
Tomaszzz 2022년 1월 31일
답변: Ankit 2022년 1월 31일
Hi all,
I have a signal like this (also attached).
I want to extract signal between first start and first end, second start and second end and so on. I am doing this like this:
load 'signal'
start_location = [147,222,302,379,458,538,620,698,777];
start_value = [-81.905,-87.408,-86.262,-85.463,-85.893,-84.910,-84.614,-83.543,-84.500]
end_location = [200,275,352,431,511,593,674,762];
end_value = [-85.369,-85.028,-82.240,-80.933,-80.943,-80.082,-83.108,-85.543];
plot(thigh_orient_y,'k')
hold on
plot(start_location,start_value,'ro')
plot(end_location,end_value,'g*')
h = legend('signal','start', 'end');
%Settings
a = start_location ;
b = end_location;
n = length(a);
%Extract data between red and green indices and put in cells
T_or_y_cycle = cell(n,1) ;
for i = 1:n
T_or_y_cycle{i} = thigh_orient_y(a(i):b(i)) ;
end
I am getting error
Index exceeds the number of array elements
Beacuse there is larger number of 'start' than 'end', as the signal is cut at the end. Can you please help how to deal with this?
The n cannot be lenght(b) beacuse the signal can be cut at the begging and start with 'end' like here:
  댓글 수: 2
Tomaszzz
Tomaszzz 2022년 1월 31일
Thanks, this does the trick, how can I accept you answer?

댓글을 달려면 로그인하십시오.

채택된 답변

Ankit
Ankit 2022년 1월 31일
load 'signal'
start_location = [147,222,302,379,458,538,620,698,777];
start_value = [-81.905,-87.408,-86.262,-85.463,-85.893,-84.910,-84.614,-83.543,-84.500]
end_location = [200,275,352,431,511,593,674,762];
end_value = [-85.369,-85.028,-82.240,-80.933,-80.943,-80.082,-83.108,-85.543];
plot(thigh_orient_y,'k')
hold on
plot(start_location,start_value,'ro')
plot(end_location,end_value,'g*')
h = legend('signal','start', 'end');
%Settings
a = start_location ;
b = end_location;
n = min(length(a),length(b)); % this will avoid extra start or end value.
%Extract data between red and green indices and put in cells
T_or_y_cycle = cell(n,1) ;
for i = 1:n
T_or_y_cycle{i} = thigh_orient_y(a(i):b(i)) ;
end

추가 답변(1개)

KSSV
KSSV 2022년 1월 31일
Your start_location is of size 1x9. Where as end_location is only 1x8. You need to put one more index in the variable end_location.

Community Treasure Hunt

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

Start Hunting!

Translated by