Reputation: 459
I am trying to find max value of a struct but max([tracks(:).matrix])
does not work. It gives me the following error: "Error using horzcat
CAT arguments dimensions are not consistent." Do you have an idea?
Here is what my struct looks like:
tracks =
1x110470 struct array with fields:
nPoints
matrix
tracks.matrix includes 3D points. For example here is
tracks(1,2).matrix:
33.727467 96.522331 27.964357
31.765503 95.983849 28.984663
30.677082 95.989578 29
Upvotes: 2
Views: 2825
Reputation: 38032
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
Upvotes: 1
Reputation: 2344
You can use array fun, followed by another max to do this:
s.x = [1 3 4];
s(2).x = [9 8];
s(3).x = [1];
maxVals = arrayfun(@(struct)max(struct.x(:)),s);
maxMaxVals = max(maxVals(:));
Or, if you want to retain the size of .x after MAX:
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{:}))
Or, if you know everything is n x 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)
Upvotes: 4
Reputation: 33864
Im not sure what you are trying to find the max of, but you can do this:
matrixConcat = [tracs.matrix]
which will give you a big concatenated list of all the matrices. You can then do max on that to find the maximum.
Let me know if this is what you were looking for otherwise i will change my answer.
Upvotes: 2