Reputation: 718
I'm writing code in which I have a 2D struct array (organism = 10x10 struct
), one of its fields consists completely of scalars, and I want to extract all of these scalars and put them into a matrix.
I tried putting it in square brackets, but instead of giving me a rectangular matrix, it gave me a long row matrix;
>> [organism(1:3,1:3).fitness]
ans =
-5 990 493 492 -5 -8 994 -5 -10
Again, I was hoping to receive
-5 990 493
492 -5 -8
994 -5 -10
I tried vertcat(organism(1:3,1:3).fitness)
, but as could be expected, it just gave me one column. I'm hoping to do this with no for loops, as this will eventually be the whole of a struct array with a size at minimum 50x50.
Is there a way to turn a field that's all scalars in a 2D struct array into a matrix with the same dimensions?
(I won't have any trouble with 40x60 turning into 60x40; I don't expect to have any difficulty transposing the matrix I get out.)
Upvotes: 2
Views: 144
Reputation: 473
You can use reshape:
scalar_matrix = reshape([organism.fitness], size(organism));
Upvotes: 1