Matlab - Two Column Matrices into One Column Matrix with rows elements

时间:2015-07-13 20:59:16

标签: matlab matrix concatenation

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

2 个答案:

答案 0 :(得分:1)

你基本上是正确的,但是num2cell([X,Y])会创建一个2x50的单元格,从而产生2x50的结构。您希望仅在第二维中拆分输入矩阵[X,Y],以便每个单元格为2x1,使用num2cell([X,Y],2)

str = struct('XYZ', num2cell([X,Y],2));

答案 1 :(得分:0)

连接两个向量,从矩阵转换为单元格,然后从单元格转换为结构数组:

str = cell2struct(mat2cell([X Y], ones(1,size(X,1)), size(X,2)+size(Y,2) ).', 'XYZ');