简体   繁体   中英

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

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

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));

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM