min/max values in 3 dimensional arrays
Show older comments
I have a 3D array in this form. [date, occurrence count per date, observed data]
example:
[15, 1, 5.23; 15, 2, 6.33; 15, 3, 4.67; 15, 4, 5.63; 16, 1, 4.98; 16, 2, 7.12; ... ]
For each date, i need to find the max number of occurrences. I can find the global min/max easily and can write loop logic to go through the matrix to find the local (per day) maximum number of occurrence, but so far can't figure out how to use find() or max() to do this. I've gotta think there's an easy/fast way.
Thanks for any help.
Mike
4 Comments
That is a 2D matrix, not a 3D array:
>> [15, 1, 5.23; 15, 2, 6.33; 15, 3, 4.67; 15, 4, 5.63; 16, 1, 4.98; 16, 2, 7.12]
ans =
15.0000 1.0000 5.2300
15.0000 2.0000 6.3300
15.0000 3.0000 4.6700
15.0000 4.0000 5.6300
16.0000 1.0000 4.9800
16.0000 2.0000 7.1200
John BG
on 7 Mar 2017
the dimensions of the input matrix really don't matter when squeezing all values with
A(:)
:]
John BG
Jan
on 7 Mar 2017
But when all columns are treated equally by using A(:), the detail "For each date, i need to find the max number of occurrences" is not considered, but you build statistics over "[date, occurrence count per date, observed data]" together.
Star Strider
on 7 Mar 2017
@Jan — You are absolutely correct.
Retaining the structure of the original matrix is essential to the correct solution to this problem, as Stephen’s and my simultaneous and nearly identical approaches illustrate.
Accepted Answer
More Answers (2)
>> X = [15, 1, 5.23; 15, 2, 6.33; 15, 3, 4.67; 15, 4, 5.63; 16, 1, 4.98; 16, 2, 7.12];
>> [uni,~,idx] = unique(X(:,1));
>> uni % these are the days
uni =
15
16
>> accumarray(idx,X(:,2),[],@max) % occurrences for each day
ans =
4
2
you can calculate other "per day" statistics easily, e.g.:
>> accumarray(idx,X(:,3),[],@mean) % mean of each day
ans =
5.4650
6.0500
etc
Star Strider
on 7 Mar 2017
It’s a 2D array. It has three columns.
One approach:
A = [15, 1, 5.23; 15, 2, 6.33; 15, 3, 4.67; 15, 4, 5.63; 16, 1, 4.98; 16, 2, 7.12;];
[Au,ia,ic] = unique(A(:,1));
DateMax = accumarray(ic, A(:,2), [], @max);
Result = [Au DateMax]
Result =
15 4
16 2
2 Comments
Mike Wilson
on 7 Mar 2017
Star Strider
on 7 Mar 2017
My pleasure!
A vote would be appreciated.
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!