Get min from group(s) of data within an array

조회 수: 7 (최근 30일)
Ludo Houben
Ludo Houben 2022년 11월 3일
답변: Ludo Houben 2022년 11월 4일
Hi All
As a newbie I'm struggeling with the following.
I have created a subset of a data set into a 'value n' x 'value m' double matrix. It contains values for several days and I need a 'minimum' value per day of a specific column. So in example:
Source: 'n' x 'm' double Result: 'v' x 4 double
2022 | 02 | 01 | 1 | ... | ... | 6 | ...
2022 | 02 | 01 | 2 | ... | ... | 6 | ...
2022 | 02 | 01 | 3 | ... | ... | 5 | ...
2022 | 02 | 02 | 1 | ... | ... | 4 | ...
2022 | 02 | 02 | 2 | ... | ... | 3 | ... ==> 2022 | 02 | 01 | 5
2022 | 02 | 02 | 3 | ... | ... | 6 | ... 2022 | 02 | 02 | 3
2022 | 02 | 03 | 1 | ... | ... | 2 | ... 2022 | 02 | 03 | 2
2022 | 02 | 03 | 2 | ... | ... | 6 | ...
2022 | 02 | 03 | 3 | ... | ... | 3 | ...
For getting the minimum value I can use the 'min()' function, but how to create the 'grouping' of the data?
Thanks in advance for any answers.
Cheers
Ludo

채택된 답변

Ludo Houben
Ludo Houben 2022년 11월 4일
Hi all
The final solution was combining both answers from Jan and Chris.
% Create table 'X'
X = [2022 02 01 1 6; ...
2022 02 01 2 6; ...
2022 02 01 3 5; ...
2022 02 02 1 4; ...
2022 02 02 2 3; ...
2022 02 02 3 6; ...
2022 02 03 1 2; ...
2022 02 03 2 6; ...
2022 02 03 3 3];
[DayInfo,~] = unique(X(:,1:3),'rows','stable'); % This creates an array 'DayInfo' containing all unique dates
temp = groupsummary(X(:,5),X(:,1:3),"min"); % This gets the minimum value of row 5 for each day
DayInfo = cat(2,DayInfo,temp); % This combines the 2 arrays
%% Combination of formula's temp + DayInfo
DayInfo = cat(2,DayInfo,groupsummary(X(:,5),X(:,1:3),"min")); % This combines the last 2 previous actions
So a big shout out to them for their help.
Regards
Ludo

추가 답변 (2개)

Cris LaPierre
Cris LaPierre 2022년 11월 3일
Use groupsummary. If you are really new to MATLAB, consider using the Compute by Group task inside a live script.
Since you haven't attached your data, here's a rough guess at what the code might be. This is written assuming your data is in a table.
dailyMin = groupsummary(tblNm,["Var1","Var2","Var3"],"min","Var7")
  댓글 수: 3
Cris LaPierre
Cris LaPierre 2022년 11월 3일
편집: Cris LaPierre 2022년 11월 3일
Consider saving your variables to a mat file and attaching it to your post using the paperclip icon.
In that case, use the Array Data syntax instead. I'm guessing at column numbers to use. You'll need to correct.
dailyMin = groupsummary(SubArc(:,7),SubArc(:,2:4),"min")
Ludo Houben
Ludo Houben 2022년 11월 3일
Hi Chris
The solution is not completely the desired format, but it gives me a good direction to continue my quest with. Thank you for your reply.
Regards
Ludo

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


Jan
Jan 2022년 11월 3일
Is the 4th row a counter for each day? Then this can be used:
(By the way, please post input data in a form, in which they can be used by copy&paste - this makes it much easier to use it in an answer)
X = [2022 02 01 1 6; ...
2022 02 01 2 6; ...
2022 02 01 3 5; ...
2022 02 02 1 4; ...
2022 02 02 2 3; ...
2022 02 02 3 6; ...
2022 02 03 1 2; ...
2022 02 03 2 6; ...
2022 02 03 3 3];
G1 = X(:, 4) == 1;
G = cumsum(double(G1));
minGValue = accumarray(G, X(:, 5), [], @min);
Result = [X(G1, 1:3), minGValue]
Result = 3×4
2022 2 1 5 2022 2 2 3 2022 2 3 2
  댓글 수: 3
Jan
Jan 2022년 11월 3일
편집: Jan 2022년 11월 3일
I'd use:
X = [2022 02 01 1 6; ...
2022 02 01 2 6; ...
2022 02 01 3 5; ...
2022 02 02 1 4; ...
2022 02 02 2 3; ...
2022 02 02 3 6; ...
2022 02 03 1 2; ...
2022 02 03 2 6; ...
2022 02 03 3 3];
[Xdate, ~, G] = unique(X(:, 1:3), 'rows', 'stable')
Xdate = 3×3
2022 2 1 2022 2 2 2022 2 3
G = 9×1
1 1 1 2 2 2 3 3 3
to obtain the group index for each set of dates.
Ludo Houben
Ludo Houben 2022년 11월 4일
Hi Jan
Thanks for the thorrow explaination. This helps me in my further development :-)
Regards
Ludo

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by