Vectorization problem for grouping entries in an array

The data below are grouped into three different subgroups based on the value of B(:,3). If, however, B(:,2) <= 1, how do I indicate that all of the entries in that subgroup share a common ID? For example, say I have the following:
B = [
1 1 1 0.1 0.2
2 0 1 0.3 0.1
3 1 1 0.1 0.2
4 2 2 0.6 0.1
5 3 2 0.3 0.7
6 1 3 0.1 0.5
7 0 3 0.5 0.1
8 1 3 0.9 0.2
]
I would like to return
C = [1 0 1];
where the first 1 indicates that the three entries (subgroup 1) with B(:,3) = 1 correspond to type 1, the next subgroup corresponds to type 0, and the third subgroup corresponds to type 0. Is there a robust way of doing this?

6 Comments

You say "the first 1 indicates that the three entries (subgroup 1) with B(:,3) = 1 correspond to type 1, the next subgroup corresponds to type 0, and the third subgroup corresponds to type 0. " Why does the second and third subgroup both correspond to "type 0"? If the third column is the group column, what column is the "type" column?
Sorry if I was unclear. They are different subgroups because they have different values for B(:,3) but the first and third subgroups belong to the same type since all of their B(:,2) values are <= 1. In contrast, the subgroup with B(:,2) > 1 correspond a different type of subgroup.
OK so why does group 2, in rows 4-5, have the same type as group 3, in rows 6-8? Why are they both type 0 when group 2 has column value less than 2 and group 3 has values of 2 or more?
What is the difference between a group and a "subgroup"?
Please precisely define
  1. group
  2. subgroup
  3. type
Please define your terminology exactly.
Please be careful with your numbers so you don't waste everyone's time. 🙂
Sure, no problem. I haven't used the term group, but if it helps to define, that would be everything. Subgroup is based on the value of B(:,3). There are three unique numbers for B(:,3) in my example, so three different subgroups. Type of subgroup is based on B(:,2). If <=1, the subgroup belongs to one type. If B(:,2) > 1 for a subgroup, the subgroup belongs to another type. Therefore, I should get C = [ 1 0 1] in this particular case returning the type for each subgroup.
So when you said "the next subgroup corresponds to type 0, and the third subgroup corresponds to type 0", meaning the second and third subgroup are both zero, that was wrong? You really meant "the third subgroup corresponds to type 1"?
And when assigning the type, you give the criteria "B(:,2) > 1" so does that mean ALL col 2 values must be like that, and if so, all rows of that subgroup are that type? OR do you check that on a row by row basis so each row will have it's own type and you might have multiple types within a subgroup. For example
B = [
1 1 1 0.1 0.2
2 0 1 0.3 0.1
3 9 1 0.1 0.2];
Would the type be
theType = B(:, 2) > 2
theType = 3×1 logical array
0 0 1
or something else?
"the third subgroup corresponds to type 1"
>> This is what I meant. Before I had a badly placed typo. So sorry about that!
"And when assigning the type, you give the criteria "B(:,2) > 1" so does that mean ALL col 2 values must be like that, and if so, all rows of that subgroup are that type? "
>> This is correct. All rows of a subgroup will be of the same type.
Does that help? I really hope so.

Sign in to comment.

Answers (1)

findgroups(B(:,2)<=1, B(:,3))

1 Comment

That returns [2 2 2 1 1 3 3 3]' rather than the array C = [1 0 1]

Sign in to comment.

Categories

Products

Release

R2021b

Asked:

on 17 Apr 2023

Commented:

on 17 Apr 2023

Community Treasure Hunt

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

Start Hunting!