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

3 次查看(过去 30 天)
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 个评论
Ankit
Ankit 2022-1-31
编辑:Ankit 2022-1-31
How about choosing the size based on minimum value of n = min(length(a),length(b)) ?
Tomaszzz
Tomaszzz 2022-1-31
编辑: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.

类别

Help CenterFile Exchange 中查找有关 Vibration Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by