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

X
Ximena · Sep 29, 2020 · 1.9K views
Question
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 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.
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Aug 25, 2026

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.webp";
"Users\MyUserName\Desktop\MyProject\MyImages\dogs1\dogs1.2.webp";
"Users\MyUserName\Desktop\MyProject\MyImages\dogs2\dogs2.1.webp"];
splitpath = split(s,filesep);
subfolder = splitpath(:,end-1);
% you can convert it to categorical if you wish.
cat_folder = categorical(subfolder);
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!