# How can I separate the data into two groups.?

10 Ansichten (letzte 30 Tage)
zhoug zho am 21 Mai 2021
Beantwortet: Kartikay Sapra am 21 Mai 2021
How can I separate the data into two groups.
1. If the first element of a column is largest number of that column, such type of columns are placed into one group.
2. If the largest number is not at the first position of that column and is present as 2nd 3rd 4thor any other position of that column, then such columns are placed in one group.
I need to form two groups of this data. Screenshot of data is attached. I highlighted few coloumn's largest number.
##### 3 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Daniel Bengtson am 21 Mai 2021
So you basically need to compare the first element in each column against the maximum value in each column. If they are equal then that column goes in the first group, otherwise the column would go in the second group.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

David Fletcher am 21 Mai 2021
Bearbeitet: David Fletcher am 21 Mai 2021
%Get index of max values in each column
[val idx]=max(data)
%Create logical vector of max values where the max value is the first in the
%column
gp1_idx=(idx==1)
%Use logical vector to index out columns for one group
group1=data(:,gp1_idx)
%Use (not)logical vector to index out columns of the other group
group2=data(:,~gp1_idx)
##### 2 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
David Fletcher am 21 Mai 2021
Bearbeitet: David Fletcher am 21 Mai 2021
If you are ignoring zeros why have you got 0 marked as the max in the first column? Not sure I get it.

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Kartikay Sapra am 21 Mai 2021
data = [-1 -2 -1; 0 -1 0; 0 0 0]
data(data==0) = NaN
[maxVal ids] = max(data)
data(isnan(data))=0
part_a = data(1:end,ids==1)
part_b = data(1:end,~(ids==1))
When finding the maximum, just keep the indices of non-zero values by making zeros 'NaN'. After that, NaN values dont interfere with max/min so after retrieving the indices, change NaN back to 0.
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by