Get cell array with childs from a cell array of identical structs

3 ビュー (過去 30 日間)
David
David 2020 年 1 月 17 日
コメント済み: David 2020 年 1 月 17 日
Dear Community,
I have the following problem. I have a cell array named "tags", each cell contains the same identical struct (please find .mat file attached). Each of this structs has two children "key" and "value" which are both strings (they could also be categorical as there is only a finite set of values).
Now I want to have a vector of indizes, telling me which of the cells contains a struct whos "value" child is equal to some querry string (e.g. water, forest).
Because my dataset it quite large I would like to solve this problem without for loops.
The problem is I don't know how to calculate a cell array containing the childs from a cell array containing structs which contain the childs.
Best regards,
David

採用された回答

Guillaume
Guillaume 2020 年 1 月 17 日
You're complicating your life by using a cell array to store scalar structures. Notwithstanding the extra memory used, it's an unnecessary level of indirection you don't need.
I would recommend converting that cell array into either a structure array or a table with actual strings (not char vectors) both of which are easier to use (table being the easiest).
Using a structure array:
tags = vertcat(tags{:}); %convert 1xN cell array of 1x1 structures with the same field into a Nx1 cell array.
%now find which elements of the structure have 'value' equal to something:
isforest = strcrmp({tags.values}, 'forest'); %logical vector. use find if you do need to convert it to indices. However logical vectors are often better.
Using a table of strings:
tags = struct2table(vertcat(tags{:})); %convert to structure then table
tags = convertvas(tags, 1:2, 'string'); %convert cell arrays of char vector into string array
isforest = tags.values == "forest"; %again use find if you need actual indices
  1 件のコメント
David
David 2020 年 1 月 17 日
Thanks! Using "vertcat" was exactly what I have been looking for.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by