简体   繁体   中英

Pulling a subset of a matrix in MATLAB

I want to cluster an array, this array contain some angle I want to calculate the difference between of these degree and select one group between this array, this group should have maximum number and the difference between the member of that should not be larger than specific number.

for example if specific number is 30 and array is

[10 20 30 40 100 120 140]

answer should be

[10 20 30 40]

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

A one-line solution:

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

b = a( abs(a-s) < s )
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM