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 个评论
Shelley Snider
Shelley Snider 2023-6-5
I'm failiar with those functions. I'm having a hard time understandng how to put them together to get what I want.
Stephen23
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
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 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by