How to merge rows of a table

8 次查看(过去 30 天)
Jiongyi Meng
Jiongyi Meng 2022-5-3
回答: Rahul 2024-9-10
I created the following table from the csv file when I created the deep learning training labels. The next step is to use the boxLabelDatastore function for processing. Before that, I can see the first column of the table, that is, the file name is repeated, which means that each training The data has multiple labels. How can I combine the lines with the same file name to achieve the effect of labeling one training data with multiple labels at the same time?

回答(1 个)

Rahul
Rahul 2024-9-10
I understand that you wish to merge those rows of the table which contain the same filename along with their labels as specified in the screenshot attached.
You can follow the following steps to achieve the desired result:
  • Use the 'findgroups' function on the 'Filename' column to group the rows by filename.
% Considering 'data' variable to be the table
% I have named the first column as 'FileName'
[G, fileNames] = findgroups(data.FileName);
  • Use the 'splitapply' function to combine the labels for each group into a single array or cell.
% I have named the second column as 'Var2'
combinedVar2= splitapply(@(x) {x}, data.Var2, G);
% I have named the third column as 'Labels'
combinedLabels = splitapply(@(x) {x}, data.Labels, G);
  • Construct a new table with unique file names and combined labels.
combinedData = table(fileNames, combinedVar2, combinedLabels, 'VariableNames', {'FileName', 'Labels'});
% 'combinedData' would consist of unique filenames with combined cell elements in the 'Var2' and 'Labels' columns
You can refer to the following Mathworks documentations to know more about these functions:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by