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

Stephen23
Stephen23 2017년 3월 7일
편집: Stephen23 2017년 3월 7일
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
the dimensions of the input matrix really don't matter when squeezing all values with
A(:)
:]
John BG
Jan
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
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

John BG
John BG 2017년 3월 7일

0 개 추천

Hi Mike
the command histogram generates all you need for this question:
1. your data
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]
.
2. extrude data from 3D to 1D
A2=A(:)
.
3. finding out amount of different occurrences
nbins=numel(unique(A2))
.
4.
h1=histogram(A2,nbins)
.
the data, as pulled out from A into A2 is contained in
h1.Data
=
15.0000
15.0000
15.0000
15.0000
16.0000
16.0000
1.0000
2.0000
3.0000
4.0000
1.0000
2.0000
5.2300
6.3300
4.6700
5.6300
4.9800
7.1200
checking amounts
isequal(numel(unique(h1.Data)),numel(unique(A2)))
=
1
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG

댓글 수: 6

Jan
Jan 2017년 3월 7일
편집: Jan 2017년 3월 7일
The question was:
data: [date, occurrence count per date, observed data]
For each date, i need to find the max number of occurrences.
I do not see, why a histogram over the complete data matrix is meaningful or solves the problem.
Stephen23
Stephen23 2017년 3월 8일
편집: Stephen23 2017년 3월 8일
@Jan Simon: it doesn't. It removes all structure and mixes the data together, giving a totally meaningless histogram. The histogram combines all data columns together (the data columns are given in the question as "date, occurrence count per date, observed data"): how is it useful to give a histogram with date numbers and observed data all mixed up? There is absolutely no way to tell if any value (e.g. 1) is a date, count, or data value, which means the histogram has no way to distinguish between these three: each histogram column contains all of these values!
See the other two answers for correct solutions to this question (i.e. that do not mix up the data).
Jan
Jan 2017년 3월 8일
@Mike Wilson: Why did you select this answer as accepted? I think only the two other suggestions solve the problem.
John BG
John BG 2017년 3월 8일
편집: John BG 2017년 3월 9일
the max number of occurrences is right the first value repeated the max amount of time of the vector h1.Data
It's 15, 4 times
The histogram helps understand where the max occurrences happen: the tallest bar.
John BG
The required output is
15 4
16 2
That is, there are two different dates, and the maximum occurrence number is needed for each.
Your code does not make appropriate computations. It would, for example, fail on
[15 16 0.123;
16 3 0.123
the required output for which would be
15 16
16 3
whereas your code would produce a histogram showing two copies of 0.123, one copy of 3, one copy of 15, and two copies of 16 -- information that is of no relevance to the question.
"the max number of occurrences is right the first value repeated the max amount of time of the vector h1.Data"
No, not with your code it is not.
First off, you assume that the maximum number of occurrences is the same as the number of occurrences of the same number that is the date. That is not given in any respect: we are not told that the occurrence numbers will be 1, 2, 3, ... up to the maximum. We are asked to find the maximum of the numbers that do occur, per date.
Even if it is the case that the occurrence numbers occur in sequence starting from 1 for any given date, consider data that looks like,
x = -1:
[3 1 x;
3 2 x;
3 3 x;
3 4 x;
4 1 x;
4 2 x;
4 3 x]
The histogram that would be produced for this would be
-1 -- 7 times
1 -- 2 times
2 -- 2 times
3 -- 6 times
4 -- 4 times
when the desired output would be
3 -- maximum occurrence 4
4 -- maximum occurrence 3
Your code confused occurrence numbers and dates, and that's not even considering the possibility of non-sequential occurrence numbers.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Stephen23
Stephen23 2017년 3월 7일
편집: Stephen23 2017년 3월 7일

1 개 추천

Perhaps using accumarray:
>> 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
Star Strider 2017년 3월 7일

1 개 추천

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

Mike Wilson
Mike Wilson 2017년 3월 7일
Roger that (on 3 column, 2D array). Great answer. Didn't know of those useful sorting/stats functions. Thanks!
Star Strider
Star Strider 2017년 3월 7일
My pleasure!
A vote would be appreciated.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품

질문:

2017년 3월 7일

댓글:

2017년 3월 9일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by