# Grouping vector elements that satisfy multiple conditions

3 views (last 30 days)
alessio morabito on 14 Mar 2021
Edited: Uday Pradhan on 18 Mar 2021
Good evening. For some time now I've been puzzling trying to figure out, as a basic matlab user, how to solve this problem: I have a column vector, A, of N elements, which are to be classified into two groups X and Z depending on whether the element satisfies a certain condition. Furthermore, each series of M consecutive elements that satisfies the condition must be grouped into a vector to be named progressively (for example, the first series of M consecutive elements that satisfies the condition of belonging to X, must be grouped into a vector X1 and so on). for example, once the elements that satisfy one or the other condition have been identified, I will find myself in a situation like A = (X, X, X, Z, X, Z, Z, Z, X, X, X); I must now group the series of at least 2 consecutive elements X into vectors Xi and into vectors Zi the series of at least 2 consecutive elements Z and, if there are isolated elements, group them in the vector that grouped the elements preceding it. in the example case, the result to be obtained would be the following:
X1 = (X, X, X, Z, X): the first 3 consecutive elements that satisfy the condition X, the isolated element that satisfies the condition Z and the element that satisfies condition X;
Z1 = (Z, Z, Z);
X2 = (X, X, X)
I have made many attempts (especially with for and if loops), but I have not been able to reach the goal. sure that someone will be pleased to deal with this question, I thank in advance anyone who can give me any advice on the matter.

Uday Pradhan on 18 Mar 2021
Edited: Uday Pradhan on 18 Mar 2021
Hi,
I have written a small script which tries to group the vector A according to the conditions you have established, please go through it. It is not perfect but might be a good starting point to what you are trying to accomplish.
%For simplification say X is 10 and Z is represented by 11
A = [10, 10, 10, 11, 10, 11, 11, 11, 10, 10, 10];
%Create a vector to hold group number
G = ones(size(A));
i = 2;
cur_group_idx = 1;
prevWasIsolated = false; %To hold true/false if the previous pt was isolated or not
while i<= length(A)
if (A(i) == A(i-1))
G(i) = cur_group_idx;
prevWasIsolated = false;
elseif prevWasIsolated
%if current element is not equal to last,check if last element was isolated
G(i) = cur_group_idx; %if yes, then current element falls in the same group as before
prevWasIsolated = false;
elseif ((i == length(A)) || (A(i) ~= A(i+1)))
%if curr element is not equal to the prev element and prev is also
%not isolated, check if curr element is the last element of A or it itself is
%isolated
G(i) = cur_group_idx;
prevWasIsolated = true;
else
%else, a new group needs to be formed
cur_group_idx = cur_group_idx + 1;
G(i) = cur_group_idx;
prevWasIsolated = false;
end
i = i + 1;
end
>> G
G =
1 1 1 1 1 2 2 2 3 3 3
This indicates the first 5 elements of A belong to group 1, then the rest group 2 and
the last 3 are group 3