Reputation: 1
I have two column matrices:
size(X) = 50 x 1 size(Y) = 50 x 1 which I got from ind2sub
I want to create a structure str such that
str(i).XYZ returns [X(i), Y(i)] for i in [1,50]
I am trying to use
str = struct('XYZ', num2cell([X,Y]));
However, I believe for this to work properly, I need to modify [X,Y] to a matrix of row vectors, each row vector being [X(i), Y(i)]. Not sure if there is a better approach
Upvotes: 0
Views: 173
Reputation: 36710
You are basically on the right way, but num2cell([X,Y])
creates a 2x50 cell, which results in a 2x50 struct. You want to split your input matrix [X,Y]
only among the second dimensions so each cell is a 2x1, use num2cell([X,Y],2)
str = struct('XYZ', num2cell([X,Y],2));
Upvotes: 1
Reputation: 112659
Concatenate the two vectors, convert from matrix to cell, and then from cell to struct array:
str = cell2struct(mat2cell([X Y], ones(1,size(X,1)), size(X,2)+size(Y,2) ).', 'XYZ');
Upvotes: 0