How can I assign values to min/max of a column within each value of another column?

2 vues (au cours des 30 derniers jours)
I have a table for which I need to programmatically identify and assign values (column 4) to the max and min Time values (column 2), for each data acquisition Device (column 1).
The earliest/minimum time for device 52 for example, is in row 1 at 8:28; that device's corresponding row at 10:47 (row 2) is the latest/maximum time. Row 1 is then identified as "Baseline" and row 2 as "Peak".
Columns 1-3 are given. The rows of the table are not necessarily ordered. I've manually filled in column 4 in the example below. There are 2 times for each device in the table.
Device time current Identifier
52 08:28 1.04 "Baseline"
52 10:47 2.02 "Peak"
42 08:23 1.03 "Baseline"
10 08:29 1.01 "Baseline"
10 10:45 2.11 "Peak"
42 10:41 2.05 "Peak"
Thanks in advance for the help!
  3 commentaires
Matt J
Matt J le 15 Oct 2021
Modifié(e) : Matt J le 15 Oct 2021
What about rows that are neither Peak nor Baseline?
A Mackay
A Mackay le 15 Oct 2021
No need to label those rows that are neither Peak nor Baseline. Leave the column 4 blank

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 15 Oct 2021
Modifié(e) : Matt J le 15 Oct 2021
Let's call your table T, then,
T=table(randi(2,10,1), randi(100,10,1), rand(10,1),'V',{'Device','Time','current'})
str=["";"Peak";"Baseline"]; %string lookup
G=findgroups(T{:,1}); %assign group labels to column 1
I=(1:numel(G))'; %enumerate rows
fun=@(t,i)deal( {(t==max(t))+2*(t==min(t))+1},{i});
%gives numeric label to every member of a fixed group: 1=non-extreme, 2=max. , 3=min.
[s,idx]=splitapply(fun ,T{:,2} ,I ,G); %apply labelling fun to each group
newcol(cell2mat(idx))=str(cell2mat(s));%Uses numeric labels to lookup string labels.
%Also restores list to its original
%order.
Tnew=[T,table(newcol(:),'V',"Identifier")] %append new column
Tnew = 10×4 table
Device Time current Identifier ______ ____ ________ __________ 1 89 0.31783 "Peak" 1 9 0.38992 "" 1 67 0.71375 "" 1 86 0.12292 "" 2 92 0.56198 "Peak" 1 89 0.45877 "Peak" 1 42 0.87733 "" 2 59 0.097834 "Baseline" 2 82 0.13001 "" 1 4 0.95036 "Baseline"
  3 commentaires
A Mackay
A Mackay le 15 Oct 2021
Sweet. Very impressive. Thanks again!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Simultaneous and Synchronized Operations dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by