MATLAB: How to group the rows of a matrix based on a grouping variable

deep learningimage analysisimage processingmachine learningmatricesmatrixmatrix arraymatrix manipulationvariablevariables

Hi everyone,
I combined three categorical arrays and obtained a 1000×3 matrix (associated with 1000 images I am working on). Basically, one of the three variables holds the path to the images, giving something along the lines of:
Users\MyUserName\Desktop\MyProject\MyImages\dogs1\dogs1.1.png
Users\MyUserName\Desktop\MyProject\MyImages\dogs1\dogs1.2.png
Users\MyUserName\Desktop\MyProject\MyImages\dogs2\dogs2.1.png
and so on.
My goal is to group the rows of the matrix according to the last subfolder (i.e. dogs1 and dogs2 in my simplified example) they are associated with under the "path" variable. I would like to do so in order to automatically perform basic stats on each group (mode, specifically).
Is this even feasible with such an "unconventional" variable? If not, is there a way to (automatically) replace the "path" array with another (normal) categorical array such as "dog1", "dog2" etc. ? This would also solve the issue.
I tried to look at different questions but couldn't find any that dealt with this issue, so I would really appreciate any help.
Thank you very much.
Best regards

Best Answer

Assuming that the folder depth is the same. you can use get the subfolder name like this.
s = ["Users\MyUserName\Desktop\MyProject\MyImages\dogs1\dogs1.1.png";
"Users\MyUserName\Desktop\MyProject\MyImages\dogs1\dogs1.2.png";
"Users\MyUserName\Desktop\MyProject\MyImages\dogs2\dogs2.1.png"];
splitpath = split(s,filesep);
subfolder = splitpath(:,end-1);
% you can convert it to categorical if you wish.
cat_folder = categorical(subfolder);