Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个结构数组,其中每个同名字段的大小相同。我想将数组上的每个字段组合成一个字段。例如:
A(1).x = [1 2 3]; A(1).y = [4 5 6]; A(2).x = [7 8 9]; A(2).y = [10 11 12];
我希望新的结构是
B.x = [1 2 3; 7 8 9]; B.y = [4 5 6; 10 11 12];
如果这使事情变得更容易,这些也可能是结构单元。
A.x生成以逗号分隔的值列表。您可以在函数调用中使用这个逗号分隔的列表,在这种情况下,每个值都被视为一个单独的参数。这非常有用,因为您可以将其传递给调用cat以连接值:
A.x
cat
B.x = cat(1, A.x);
您必须对每个字段执行相同的操作。要自动执行此操作,您可以迭代fieldnames(A):
fieldnames(A)
for name = fieldnames(A).' B.(name{1}) = cat(1, A.(name{1})); end