min/max values in 3 dimensional arrays
이전 댓글 표시
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
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
2017년 3월 7일
the dimensions of the input matrix really don't matter when squeezing all values with
A(:)
:]
John BG
Jan
2017년 3월 7일
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
2017년 3월 7일
@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.
채택된 답변
추가 답변 (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
2017년 3월 7일
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
카테고리
도움말 센터 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
