1

Is there a way to convert a struct (2 fields with 52 variables each) to a matrix (2x52)? Thank you

struct:

    sym (1x53)
    prob (1x53)

I have tried the following which gives me a 1 x 1 cell array

symProb = reshape({x.sym}, size(53)); 

I have also tried struct2cell which does the same.

4

3 回答 3

6

可能最简单的事情(因为它只有两个字段)是简单地将它们沿着第一个维度连接使用cat

result = cat(1, x.sym, x.prob);

或者你可以使用[];

result = [x.sym; x.prob]

如果你想要一个更通用的解决方案,你可以使用struct2array一些重塑

result = reshape(struct2array(x), [], numel(x)).';

请注意,所有这些都假定symprob中的数据实际上是相同的数据类型,因此能够放置在同一个数组中,否则元胞数组是保存这两个字段的唯一方法。

此外,您的代码会生成一个 1 x 1 单元格数组,因为您将数据包装x.sym 一个1 x 1单元格数组中。

于 2017-01-08T23:17:47.697 回答
0

你可以使用这个:

cell2mat(struct2cell(YourStructure))
于 2018-11-02T13:16:17.720 回答
0

另一种方法如下:

symVec = [x.sym(:)]
probVec = [x.prob(:)
于 2017-12-05T16:28:14.103 回答