selecting certain elements of an array

20 次查看(过去 30 天)
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

回答(1 个)

Kodavati Mahendra
Kodavati Mahendra 2017-12-26
编辑:Kodavati Mahendra 2017-12-26
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 |

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by