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

1 回表示 (過去 30 日間)
M J
M J 2020 年 8 月 30 日
コメント済み: M J 2020 年 9 月 7 日
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

採用された回答

Mohammad Sami
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);
  1 件のコメント
M J
M J 2020 年 9 月 7 日
Thanks! Led me to the solution.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by