Find minimum values based on unique number

I have a dataset of temperatures collected over multiple depth profiles (1-147 profiles). The data is all listed in one long table, not by each profile (attached).
Each profile has a different temperature minimum, and I want to find this minimum for each profile, and colour all of the temperatures above this in grey in a figure (essentially discarding them).
  1. Evidently I'm going about this the wrong way as my output (T_min) is all the same number (see code below).
  2. Once I have the T_min for each profile, when I do a scatter plot, how can I colour each dot less than the T_min - for that particular profile - grey?
Thanks in advance - sorry if this isn't very clear.
j=1;
for i=1:length(dives)
T_min(j) = nanmin(SG579_FINAL_table_MF.Cons_temp(dives));
j=j+1;
end

 Réponse acceptée

hello
the provided mat file contains 492 different profiles (and not 147)
my suggestion so far :
load('matlab.mat')
prof = Prof_temp.Profile_num;
prof_unic = unique(prof);
for k=1:length(prof_unic)
ind = (prof == prof_unic(k));
T_min(k) = min(Prof_temp.Temp(ind),[],'omitnan');
end
plot(prof_unic,T_min);
xlabel('Profile #')
ylabel('Average temperature')

10 commentaires

DD_2023
DD_2023 le 15 Mai 2024
Apologies, I tweaked the data before I shared it and forgot to update my text.
Thank you so much, your code has highlights the minimum values in each profile, exactly as I wanted!
I was now hoping to plot a scatter plot of the data against another variable (called S), but colour the points where the points less than the T min for that profile are coloured differently.
Mathieu NOE
Mathieu NOE le 15 Mai 2024
and can you provide this S data ?
DD_2023
DD_2023 le 15 Mai 2024
Of course, and as you may have seen below I have misexplained what I actually want to do.
This is my aim:
T_min for each profile will be located at a certain depth (called Pres, and the T_min Pres will differ between profiles). In the scatter plot of T and S, I wanted to colour any dots shallower than the T_min Pres (differing between profiles) grey.
if I have understood correctly maybe then this is it :
load('matlab.mat')
load('Pres.mat')
load('S.mat')
prof = Prof_temp.Profile_num;
prof_unic = unique(prof);
indgrey = [];
for k=1:length(prof_unic)
ind = (prof == prof_unic(k));
[T_min(k),indm] = min(Prof_temp.Temp(ind),[],'omitnan');
Pres_of_Tmin(k) = Pres(indm);
% indices of data to put in grey
indgrey = [indgrey; find(Pres(ind)<Pres_of_Tmin(k))]; % find for each profile the data for which Pres is lower than Pres_of_Tmin
% and concatenate those indexes
end
indnongrey = (1:numel(prof));
indnongrey(indgrey) = [];
scatter(S(indnongrey),Pres(indnongrey),5); % "non grey" elements
hold on
grayColor = [.7 .7 .7];
scatter(S(indgrey),Pres(indgrey),5,grayColor); % "grey" elements
hold off
xlabel('S')
ylabel('Temp')
DD_2023
DD_2023 le 15 Mai 2024
Thanks so much, this code works with my data, but looks like it is maybe only applying to one profile at a time rather than finding the depth minimum T for every profile and greying out all data shallower than that?
Apologies if it isn't doing that - I just would have expected more points to be grey
Mathieu NOE
Mathieu NOE le 15 Mai 2024
the code - hopefully - should do what i put in the comment (and what I understood from your query but maybe I'm wrong)
% find for each profile the data for which Pres is lower than Pres_of_Tmin
% and concatenate those indexes
DD_2023
DD_2023 le 15 Mai 2024
I don't think its far off, but I would expect most data at a pressure less than somewhere between 50 - 200 m to be grey. But I don't want to take up any more of your time, I'm sure I can fiddle with what you've done for me so far, it's a huge help - thank you!
I have now a slight doubt when you say : one profile at a time rather than finding the depth minimum T for every profile and greying out all data shallower than that?
does "all data" mean data of each profile or all together ?
my code above was supposing greying within each profile
if it's the second option, the new code would be - hopefully again without error :
load('matlab.mat')
load('Pres.mat')
load('S.mat')
prof = Prof_temp.Profile_num;
prof_unic = unique(prof);
indgrey = [];
for k=1:length(prof_unic)
ind = (prof == prof_unic(k));
[T_min(k),indm] = min(Prof_temp.Temp(ind),[],'omitnan');
Pres_of_Tmin(k) = Pres(indm);
% indices of data to put in grey
indgrey = [indgrey; find(Pres<Pres_of_Tmin(k))]; % find the entire data which Pres is lower than Pres_of_Tmin
% and concatenate those indexes
end
indgrey = unique(indgrey);
indnongrey = (1:numel(prof));
indnongrey(indgrey) = [];
scatter(S(indnongrey),Pres(indnongrey),5); % "non grey" elements
hold on
grayColor = [.7 .7 .7];
scatter(S(indgrey),Pres(indgrey),5,grayColor); % "grey" elements
hold off
xlabel('S')
ylabel('Temp')
DD_2023
DD_2023 le 15 Mai 2024
Thank you! This looks more like what I was expecting. Apologies for my lack of clarity :)
Mathieu NOE
Mathieu NOE le 15 Mai 2024
ok - glad we finally converged to the solution !! :)

Connectez-vous pour commenter.

Plus de réponses (1)

P = load('matlab.mat').Prof_temp
P = 460116x2 table
Profile_num Temp ___________ _______ 1 0.73595 1 0.73338 2 0.73372 2 0.72239 2 0.73413 2 0.7322 2 0.73786 2 0.73755 2 0.73594 2 0.74415 2 0.7336 2 0.72792 2 0.74704 2 0.75886 2 0.75926 2 0.7642
S = groupsummary(P,"Profile_num","min","Temp")
S = 492x3 table
Profile_num GroupCount min_Temp ___________ __________ __________ 1 2 0.73338 2 144 0.65173 3 2 0.73312 4 144 0.69801 5 239 0.36951 6 78 0.35654 7 79 0.70014 8 160 0.66216 9 156 0.013414 10 205 0.013662 11 442 0.00084435 12 626 0.028878 13 705 0.025135 14 959 0.015209 15 683 0.026312 16 915 -0.058026
plot(S.Profile_num,S.min_Temp)

4 commentaires

DD_2023
DD_2023 le 15 Mai 2024
Thank you so much!
I was now hoping to plot a scatter plot of the data against another variable (called S), but colour the points where the points less than the T min for that associated profile number are coloured differently. (Sorry, I know this is confusing wording).
Stephen23
Stephen23 le 15 Mai 2024
Modifié(e) : Stephen23 le 15 Mai 2024
" colour the points where the points less than the T min for that associated profile number are coloured differently."
I guess you mean where the S-values are less thatn T-min (because of course no Temp values should be less than T-min.
P = load('matlab.mat').Prof_temp
P = 460116x2 table
Profile_num Temp ___________ _______ 1 0.73595 1 0.73338 2 0.73372 2 0.72239 2 0.73413 2 0.7322 2 0.73786 2 0.73755 2 0.73594 2 0.74415 2 0.7336 2 0.72792 2 0.74704 2 0.75886 2 0.75926 2 0.7642
S = randn(size(P.Temp))
S = 460116x1
-1.9744 -0.7961 -0.0034 -0.2607 -0.4320 -1.1208 0.1508 0.5829 0.5262 -1.6224
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
V = grouptransform(P.Temp,P.Profile_num,@min);
X = S<V;
scatter(S(X),P.Temp(X),23,[1,1,1]./2)
hold on
scatter(S(~X),P.Temp(~X),23)
xlabel('S')
ylabel('Temp')
DD_2023
DD_2023 le 15 Mai 2024
Sorry you make an excellent point and have made me realise I've completely messed up my explanation.
T_min for each profile will be located at a certain depth (called Pres, and the T_min Pres will differ between profiles). In the scatter plot of T and S, I wanted to colour any dots shallower than the T_min Pres (differing between profiles) grey.
DD_2023
DD_2023 le 15 Mai 2024
Here are the data for Pres and S

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Performance dans Centre d'aide et File Exchange

Produits

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by