Extraction of a subset of a matrix in MATLAB

I want to group an array, this array contains some angle. I want to calculate the difference between these degrees and choose one group between this array, this group should have a maximum number, and the difference between a member of this should not be more than a certain number.

for example, if a specific number is 30 and an array

[10 20 30 40 100 120 140]

the answer should be

[10 20 30 40]

100- 30 > = 30 , so it is not included.

+2
source share
2 answers

One line solution:

a = [10 20 30 40 100 120 140];
s = 30;

b = a( abs(a-s) < s )
+5
source
a = [10 20 30 40 100 120 140]; #initial array
b = []; #result array
s = 30;
for i = 1:length(a)
    if abs(a(i) - s) < s
        b = [b a(i)];
    end
end
0
source

Source: https://habr.com/ru/post/1791900/


All Articles