user374788
user374788

Reputation: 21

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.

Upvotes: 2

Views: 2133

Answers (2)

Amro
Amro

Reputation: 124563

A one-line solution:

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

b = a( abs(a-s) < s )

Upvotes: 5

tlayton
tlayton

Reputation: 1777

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

Upvotes: 0

Related Questions