Index and select rows from table

A table includes columns with numerical and categorical columns (see attachement)
How can I get multiple subtables based on the categories of a column (e.g., col5)?
Expected output: 4 different tables (table1includes rows with "h_001" in col5... table4 includes rows with "r_041" in col5)
I could not use the following option when the columns have categorical data:
https://ch.mathworks.com/matlabcentral/answers/481574-how-to-get-the-index-of-a-value-in-a-table

댓글 수: 9

Dyuman Joshi
Dyuman Joshi 2023년 11월 21일
Dynamically naming variables is not recommended - TUTORIAL: Why Variables Should Not Be Named Dynamically (eval)
What do you want to do with the different tables?
Thanks @Dyuman Joshi,
I need to split the original data based on the categories of col5. The following code works:
index1=find(data.Col5=='h_001');
index2=find(data.Col5=='h_022');
index3=find(data.Col5=='h_045');
index4=find(data.Col5=='r_041');
data1=data(index1,:);
data2=data(index2,:);
data3=data(index3,:);
data4=data(index4,:);
However, I wonder whether there is any further alternative to simplify the code. My data has 600 categories in one column of the table.
I am looking for something more efficient than the code above. Perhaps 1 line of code per category?
Dyuman Joshi
Dyuman Joshi 2023년 11월 21일
If you have 600 categories, you will have 600 sub tables. That will be a nightmare to work with.
Did you read the link I attached in my comment above?
I'll reiterate - What do you want to do with the 600 different tables? Are there any operations you would like to perform on them?
julian gaviria
julian gaviria 2023년 11월 21일
@Dyuman Joshi wrote: Are there any operations you would like to perform on them?
Yes, I will obtain a single value from each table...
Stephen23
Stephen23 2023년 11월 21일
ISMEMBER
Dyuman Joshi
Dyuman Joshi 2023년 11월 21일
"Yes, I will obtain a single value from each table..."
Which value? Max/Min/Median/Mean? or something else?
@Stephen23 I don't see how
ismember()
would be more efficient. It would require at least 2 lines per categorie:
https://ch.mathworks.com/help/matlab/ref/double.ismember.html#d126e855390
Stephen23
Stephen23 2023년 11월 21일
편집: Stephen23 2023년 11월 21일
"I don't see how ismember() would be more efficient. It would require at least 2 lines per categorie"
Whatever way you do it, processing every category individually will be inefficient (in terms of your time writing and/or runtime).
That is why I suggested ISMEMBER, so that you can do all categories at once. Three lines of code, done.
Do not store each category individually. That is not how MATLAB works, you need to learn how to use vectors, matrices, and arrays. Start by placing all of the categories into one array (e.g. a string array), then use one ISMEMBER call. Read the ISMEMBER documentation carefully.
Another option would be to use one of the JOIN family.
Cris LaPierre
Cris LaPierre 2023년 11월 21일
Do not confuse the number of lines of code with efficiency.

댓글을 달려면 로그인하십시오.

답변 (1개)

Peter Perkins
Peter Perkins 2023년 11월 27일

0 개 추천

"I need to split the original data based on the categories of col5."
You probably do not want/need to do that. Take a look at the rowfun function. Write a function to do what you want with each subset of your data, then use rowfun to apply that function based on groups defined by col5.
t = table([1;1;1;2;2;3;3;3],rand(8,1),rand(8,1),VariableNames=["G" "X" "Y"])
t = 8×3 table
G X Y _ _______ _______ 1 0.50908 0.09642 1 0.46628 0.9575 1 0.99625 0.40089 2 0.63605 0.70043 2 0.81209 0.49475 3 0.38157 0.51853 3 0.74877 0.66036 3 0.85878 0.91136
myFun = @(x,y) mean(x) - mean(y);
rowfun(myFun,t,GroupingVariables="G")
ans = 3×3 table
G GroupCount Var3 _ __________ _________ 1 3 0.17227 2 2 0.12648 3 3 -0.033704
Lots of other functions, like groupsummary, similarly do not require you to split your data up. As others have said, that's usually a bad idea and unnecessary.

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2023년 11월 21일

답변:

2023년 11월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by