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

4 ビュー (過去 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 件のコメント
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 Center および File ExchangeVibration Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by