How to group the rows of a matrix based on a grouping variable ?
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Mohammad Sami
2020-8-31
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);
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!