How to make group of ones more systematic approach?

1 回表示 (過去 30 日間)
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2020 年 2 月 21 日
回答済み: Walter Roberson 2020 年 2 月 22 日
I have this groups of ones:
group = [ ones(size(Ribboned_100.unpatterned_B1.(string(var(i))))); 2 * ones(size(Ribboned_100.unpatterned_B10.(string(var(i))))); ...
3 * ones(size(Ribboned_100.unpatterned_He.(string(var(i))))); 4 * ones(size(Ribboned_100.unpatterned_MD.(string(var(i)))));...
5 * ones(size(Ribboned_100.unpatterned_HT.(string(var(i))))); 6 * ones(size(Ribboned_100.unpatterned_BT.(string(var(i)))));
7 * ones(size(Ribboned_100.unpatterned_NI.(string(var(i)))))]
and I have this set of strings here:
XT= {'B1','B10','He','MD','HT','BT','NI'};
I think the way I make "group" above is not efficient and there should be a way to make the "group" systematically using "XT".
Any suggestion?
  1 件のコメント
Spencer Chen
Spencer Chen 2020 年 2 月 21 日
You can loop or use arrayfun to make it more systematic, but not sure if it will be more efficient. This involves assigning each set of ones to a cell then use cat() to concatenate them.
That's my limit of Matlab without know more about your data structure.
Blessings,
Spencer

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

採用された回答

Walter Roberson
Walter Roberson 2020 年 2 月 22 日
Before the loop, struct2cell(Ribboned_100) . Then look at the fieldnames() of Ribboned_100, and match those against the names from XT, getting the respective indices. Extract those cells from the cell array.
Now you can loop over i, extracting the fields named by var{i} from those particular cells.
All of this is only likely to be more efficient than what you have if the maximum index is large.
If the var{i} between them cover all of the fields, then you could reorderfields() of those cell arrays according to var order, and then cell2mat(), after which you would be doing numeric indexing instead of struct reference.

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by