Bensa
Bensa

Reputation: 139

MATLAB: Find function within range

How can I use the find function within specific ranges.

Say, I have an array arr1 with random values. I have the start & end indices of the portions I'd like to analyze (in this example, I want to find the first occurrence for when the value is larger than 0.8)

How could the find function be used here with start and end indices and the condition as well?

For example:

arr1 = rand(1000,1);
start_ind = [100;500;850];
end_ind = [160;620;925];

for i = 1:length(start_ind)
    output = find(arr1(start_ind(i):end_ind(i)) >=0.8); % ????
end 

Much appreciated,

Upvotes: 0

Views: 154

Answers (1)

nirvana-msu
nirvana-msu

Reputation: 4077

Use the second argument of find to only get the first match. You can then shift indices by adding start_ind - 1:

arr1 = rand(1000,1);
start_ind = [100; 500; 850];
end_ind = [160; 620; 925];

output = zeros(length(start_ind), 1);
for i = 1:length(start_ind)
    output(i) = find(arr1(start_ind(i):end_ind(i)) >=0.8, 1) + start_ind(i) - 1;
end 

Upvotes: 1

Related Questions