How to make group of ones more systematic approach?

1 visualización (últimos 30 días)
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 comentario
Spencer Chen
Spencer Chen el 21 de Feb. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 22 de Feb. de 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.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by