# Comparing array with any element of the array

1 view (last 30 days)
betul uslu on 18 Dec 2019
Commented: Guillaume on 18 Dec 2019
Hello All.
I have vector named classes
classes =
1
2
I need to compare all classes element with
D = dataset(dataset(:,p)==X,:);
Briefly equation above should compareing with all classes elements, for my example X should be 1 or 2
X can be any element of classes vector, in my code i compare p th element of the dataset array with X , X is one of the elemeny of classes vector.
I just create a example classes matrix it can be any array.

Show 1 older comment
betul uslu on 18 Dec 2019
You are right, it is very hard to explain, but i will try, for example X can be any element of classes vector, in my code i compare p th element of the dataset array with X , X is one of the elemeny of classes vector. I use compare for equality 1,2 can be equal to X.
Guillaume on 18 Dec 2019
As I wrote please provide an example of all inputs and desired output.
betul uslu on 18 Dec 2019
I have dataset called 'dataset'
I also have a vector calles 'classes'
classes =
1
2
I generate classes vector from my dataset,
I need do divide my dataset in to number of element of classes vector (for my example i need to divide 2). To do this i use.
D = dataset(dataset(:,p)==X,:);
X on the above code is members in 'classes vector'
What i need is;
I need to divide 2 parts to my matrix 'dataset' order of dividing is; Matrices that their 4th (p th) colums are equal to 1 and 4th (p th) colums are equal to 2. That 1 and 2 should be coming from 'classes' vector .
Disire output is
First matrix
Second matrix
I hope i can explain everything briefly.

Guillaume on 18 Dec 2019
Ok, now I understand what you want to do.
Note that datasets have been deprecated for a while and it's recommended you use tables instead. I'm not familiar with datasets and I don't have the toolbox so the following is for tables. You can easily transform your dataset into a table with dataset2table.
First, I'll note that typically splitting a table into several tables is rarely useful. Matlab has plenty of functions that allows you to perform group statistics according to a particular column. These work on a single table. See groupsummary, grouptransform, groupfilter, etc.
If you really want to split the table into multiple tables:
g = findgroup(yourtable(:, p));
splittables = splitapply(@(rows) {yourtable(rows, :)}, (1:height(yourtable)).', g); %split table into cell array of tables