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

2 views (last 30 days)
Hi everyone,
I combined three categorical arrays and obtained a 1000x3 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

Accepted Answer

Mohammad Sami
Mohammad Sami on 31 Aug 2020
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);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!