Clear Filters
Clear Filters

selecting certain elements of an array

28 views (last 30 days)
SSG_newbiecoder
SSG_newbiecoder on 26 Dec 2017
Edited: Kodavati Mahendra on 26 Dec 2017
hello, I have an array of 200 elements. I'm trying to apply certain conditions to the elements in the array. I have to select 20 elements to the left and right of the first element that is greater than 0. But if this element doesn't have 20 elements to say, the left side(for example, say 10th element is greater than zero), still I have to choose how much ever values there are to the left. how can I do this

Answers (1)

Kodavati Mahendra
Kodavati Mahendra on 26 Dec 2017
Edited: Kodavati Mahendra on 26 Dec 2017
s = -100:100;
A = find(s>0);
s(max(A(1)-20,1):A(1)+20)
|Lets say S is the array of 200 elements.
Use find function to find all the values that satisfy your condition (value>0),
A array has indices of all values that are greater than 0 in S.
In case of 10th element greater than 0, I used max(A(1)-20,1) to make sure that 1 start from the first element
Hope this helps, I am also a newbie coder :-P |

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!