min/max values in 3 dimensional arrays
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mike Wilson
am 7 Mär. 2017
Kommentiert: Walter Roberson
am 9 Mär. 2017
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 Kommentare
Jan
am 7 Mär. 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
am 7 Mär. 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.
Akzeptierte Antwort
John BG
am 7 Mär. 2017
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 Kommentare
Walter Roberson
am 9 Mär. 2017
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.
Walter Roberson
am 9 Mär. 2017
"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.
Weitere Antworten (2)
Stephen23
am 7 Mär. 2017
Bearbeitet: Stephen23
am 7 Mär. 2017
>> 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
0 Kommentare
Star Strider
am 7 Mär. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!