Using the fields of a structure to index through a vector and generate a resulting structure
2 views (last 30 days)
Show older comments
dataArray = [21,22,23,24,25,26,27,28,29,30];
structureOfIndexes.a1 = [2,5,8];
structureOfIndexes.a2 = [3,4];
structureOfIndexes.a3 = [1,2,3,5,9];
% How do I use the structure of indexes to generate the 'resultStructure'(seen below)
resultStructure.a1 = [22,25,28];
resultStructure.a2 = [23,24];
resultStructure.a3 = [21,22,23,25,29];
% without using a for-loop to index through a1,a2,a3
0 Comments
Accepted Answer
Eric Delgado
on 27 Sep 2022
dataArray = [21,22,23,24,25,26,27,28,29,30];
structureOfIndexes.a1 = [2,5,8];
structureOfIndexes.a2 = [3,4];
structureOfIndexes.a3 = [1,2,3,5,9];
resultStructure = structfun(@(x) dataArray(x), structureOfIndexes, "UniformOutput", false)
More Answers (0)
See Also
Categories
Find more on Structures 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!