How do I extract a structure array from a single dynamic structure without using for loops?
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2023-6-5
"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,:);
采纳的回答
Matt J
2023-6-6
编辑:Matt J
2023-6-6
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!