How to make group of ones more systematic approach?

1 view (last 30 days)
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 Comment
Spencer Chen
Spencer Chen on 21 Feb 2020
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

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 22 Feb 2020
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.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!