How do I match corresponding rows of class labels to data

I have 838 by 400 matrix as my data, I also have a 275 by 2 file which contains my class labels but they are not organised to match the exact sample rows of the data, I need help with a code to match both so that the sample rows in the data matches the rows in the class labels. Any row that has no corresponding class lebels can be deleted. I need to end up with 275 by 400 data.
Note: The file with the data and the file with the class labels both have similar sample ID column to help idenify the sample rows.
Kinldy let me know if you need more clarification
Thank You

댓글 수: 2

Of course we do - you didn't attach any data or screenshots or images or anything.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
In the meantime, see ismember or join
Thanks for respnding promptly, here is the data in excel attached(Data_to_match) and the labels(GrpLabel_to_match) to match with the corresponding rows.
Thanks a lot for your help

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

 채택된 답변

Debadipto
Debadipto 2022년 7월 11일

0 개 추천

As per my understanding, you have two tables, table Data_to_match (838 by 400) and table GrpLabel_to_match (275 by 2). You want to keep only those rows in Data_to_match whose SampleID is present in GrpLabel_to_match and remove the other rows. To achieve this you can use the following code:
Data_to_match = Data_to_match(contains(Data_to_match.SampleID, GrpLabel_to_match.SampleID), :);
Please note that the dimension of the updated table Data_to_match will not be exactly 275 by 400 if the table GrpLabel_to_match contains any duplicate entries.

댓글 수: 3

Hi Debadipto,
Thanks a lot for responding.Yes that is exactly what I want to achieve, but the code fails with the error message .........I cant index into Sample ID with this data type.
I have converted the tables to cell arrays, can you help with the code on the same data I shared but as cell arrays instead of tables?.
Thank You
Assuming the two tables have already been converted to cell arrays, you can use the following code to join the two cell arrays:
A = cellstr(GrpLabel_to_match(:,1));
B = cellstr(Data_to_match(:,1));
Data_to_match = Data_to_match(ismember(A, B), :)
Thank You very much Debadipto. Perfect code to solve my problem.

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

추가 답변 (0개)

카테고리

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

제품

릴리스

R2020b

태그

질문:

NCA
2022년 7월 8일

댓글:

NCA
2022년 7월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by