2

我试图找到一个结构的最大值但max([tracks(:).matrix])不起作用。它给了我以下错误:“错误使用 horzcat CAT 参数尺寸不一致。” 你有想法吗?

这是我的结构的样子:

tracks = 

1x110470 struct array with fields:
    nPoints
    matrix

track.matrix 包括 3D 点。例如这里是

tracks(1,2).matrix:

33.727467   96.522331   27.964357
31.765503   95.983849   28.984663
30.677082   95.989578   29
4

3 回答 3

4

您可以使用数组 fun,然后使用另一个最大值来执行此操作:

s.x = [1 3 4];
s(2).x = [9 8];
s(3).x = [1];

maxVals = arrayfun(@(struct)max(struct.x(:)),s);

maxMaxVals = max(maxVals(:));

或者,如果您想在 MAX 之后保留 .x 的大小:

s.x = [1 3 4];
s(2).x = [9 8 3];
s(3).x = [1 2 2; 3 2 3];

maxVals = arrayfun(@(struct)max(struct.x,[],1),s,'uniformoutput',false);

maxMaxVals = max(cat(1,maxVals{:}))

或者,如果你知道一切都是 nx 3

s.x = [1 3 4];
s(2).x = [9 8 3];
s(3).x = [1 2 2; 3 2 3];
matrix = cat(1,s.x)
maxVals = max(matrix)
于 2012-11-27T01:10:53.063 回答
2

我不确定您要查找的最大值是什么,但是您可以这样做:

matrixConcat = [tracs.matrix]

这将为您提供所有矩阵的大串联列表。然后,您可以对其进行 max 以找到最大值。

让我知道这是否是您正在寻找的东西,否则我会改变我的答案。

于 2012-11-27T00:35:22.677 回答
1

You can't use [] because the sizes of all tracks.matrix are different, hence the concatenation fails.

You can however use {} to concatenate to cell:

% example structure
t = struct(...
    'matrix', cellfun(@(x)rand( randi([1 5])), cell(1, 30), 'uni', 0))


% find the maximum of all these data    
M = max( cellfun(@(x)max(x(:)), {t.matrix}) );

Now, if you don't want to find the overall maximum, but the maximum per column (supposing you have (x,y,z) coordinates in each column, you should do

% example data
tracks = struct(...
    'matrix', {rand(2,3) rand(4,3)})

% compute column-wise max 
M = max( cat(1, tracks.matrix) )

This works because calling tracks.matrix when tracks is a multi-dimensional structure is equal to expanding the contents of a cell-array:

tracks.matrix         % call without capture equates to:

C = {tracks.matrix};  % create cell
C{:}                  % expand cell contents
于 2012-11-27T06:49:02.040 回答