Clear Filters
Clear Filters

How to merge rows of a table

5 views (last 30 days)
Jiongyi Meng
Jiongyi Meng on 3 May 2022
Answered: Rahul on 10 Sep 2024
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?

Answers (1)

Rahul
Rahul on 10 Sep 2024
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!

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!