How to make group of ones more systematic approach?

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by