How do I extract a structure array from a single dynamic structure without using for loops?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Im trying to derive a structure array from a single structure without using for loops.
1) I make n copies of the original structure, primal
primalSP = repmat(primal,n,1)
2) I have n sets of m states in the struct primal.states. The size of primal.states is (n*m, nodes) . I want the first set of m states to go to the first structure array, and the second set to the second structure array and so on until all n sets are assigned, like this
primalSP(1).states = primal.states(1:n,:);
primalSP(2).states = primal.states(n+1:2*n,:);
...
Is there a way to do this without a for loop where n is dynamic?
3 commentaires
Stephen23
le 5 Juin 2023
"I want the first set of m states to go to the first structure array, and the second set to the second structure array and so on until all n sets are assigned"
Then your example should be:
primalSP(1).states = primal.states(1:m,:);
primalSP(2).states = primal.states(m+1:2*m,:);
Réponse acceptée
Matt J
le 6 Juin 2023
Modifié(e) : Matt J
le 6 Juin 2023
There is no way to avoid for-loop speed when dealing with structs and cells. Below is a way to abbreviate the syntax, but both num2cell.m and mat2cell.m have for-loops within them, as you will see if you read those files.
stateCell = num2cell(primal.states,1);
[primalSP(1:n).states] = deal(stateCell{:});
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Structures dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!