Correlating values from a table using a for loop

1 view (last 30 days)
I have a table that has hourly data (t) and by day (d) of chlorophyll concentrations. I am trying to find the maximum value of Chlorophyll of the surface and bottom values for each day as well as their corresponding depths and times. Does anyone know how to do this in a for loop??? I used varfun and @max in a table set up but now moving into larger data sets, a for loop would be a better route.
T1=table(d,t,Chl_Bottom, depth_chl_bottom, Chl_Surface, depth_chl_surface);
k=0;
for i = 1:length(d)
k=k+1;
smax= find(max(Chl_Surface(i))); %find the max of Chl_Surface by day
bmax = find(max(Chl_Bottom(i))); %find the max of Chl_Bottom by day
%find the time of the day when the peak is at its max
%find the depth of the day when the peak is at its max
end

Answers (3)

Mathieu NOE
Mathieu NOE on 2 Nov 2020
hello
hope my code can further help you - you don't need any for loop to find the max value (and the corresponding time index) of a vector.
enjoy
T = readtable('data.csv')
C = table2cell(T);
% data organized as follows : Date,time,depth_chl_bottom,Chl_Bottom,depth_chl_surface,Chl_Surface
Date = C(:,1);
time = C(:,2);
depth_chl_bottom = cell2mat(C(:,3));
Chl_Bottom = cell2mat(C(:,4));
depth_chl_surface = cell2mat(C(:,5));
Chl_Surface = cell2mat(C(:,6));
% % T1=table(d,t,Chl_Bottom, depth_chl_bottom, Chl_Surface, depth_chl_surface);
% k=0;
% for i = 1:length(Date)
% k=k+1;
% smax= find(max(Chl_Surface(i))); %find the max of Chl_Surface by day
% bmax = find(max(Chl_Bottom(i))); %find the max of Chl_Bottom by day
% %find the time of the day when the peak is at its max
% %find the depth of the day when the peak is at its max
% end
[Chl_Surface_max_value,time_index_max_Chl_Surface] = max(Chl_Surface);
[Chl_Bottom_max_value,time_index_max_Chl_Bottom] = max(Chl_Bottom);
x_axis = 1:length(Date);
figure(1),
subplot(2,1,1),plot(x_axis,Chl_Surface,'b',x_axis(time_index_max_Chl_Surface),Chl_Surface_max_value,'ro');
subplot(2,1,2),plot(x_axis,Chl_Bottom,'b',x_axis(time_index_max_Chl_Bottom),Chl_Bottom_max_value,'ro');
  1 Comment
Sourabh Kondapaka
Sourabh Kondapaka on 4 Nov 2020
When reading the data as a table we can directly work with the table instead of converting it to a different format.
Jacqueline Chrabot had asked for a max Chl_Surface and Chl_Bottom for each day. Your code will find max of the aforementioned values for the entire dataset.

Sign in to comment.


Sourabh Kondapaka
Sourabh Kondapaka on 4 Nov 2020
The following code will help you in achieving what you want:
%Read 'Data.csv' as a table
T =readtable('DATA.csv');
% As you want max values of Chl_Surface and Chl_Bottom for each day, we will need
% all the unique dates in the table first
unique_dates = unique(T.Date);
% Iterating over each unique date and finding max Chl_Surface and Chl_Bottom for that day:
for i = 1 : length(unique_dates)
%For the current unique day
fprintf("For the day %s is \n", string(unique_dates(i)));
% Extracting a part of T for the current date.
t = T(T.Date == unique_dates(i), :);
% Extracting max Chl_Surface value and the index where it occurs
[maxSurfaceValue, maxSurfaceIdx] = max(t.Chl_Surface);
% Display the max Chl_Surface value and the depth and time it occurs
disp("Max Chl_Surface is :");
disp(T(maxSurfaceIdx, :));
% Extracting max Chl_Bottom value and the index where it occurs
[maxBottomValue, maxBottomIdx] = max(t.Chl_Bottom);
% Display the max Chl_Bottom value and the depth and time it occurs
disp("Max Chl_Bottom is : ");
disp(T(maxBottomIdx, :));
end
A similar question about extracting max value in a column has been asked here
Check the documention for "max()" function here
  5 Comments
Jacqueline Chrabot
Jacqueline Chrabot on 16 Nov 2020
There are multiple peaks during a day for each the chlorophyll surface reading and chlorophyll bottom reading. You originally helped provide me a script finding the mean of those peaks for the surface and bottom per day (and the corresponding depth and time associated with the mean) But what if I wanted just the first peak and the last peak of the surface and bottom per day. I'm thinking that the first peak would be more accurate of a reading rather than taking the mean ( as taking the mean would include maybe some noise).
Jacqueline Chrabot
Jacqueline Chrabot on 16 Nov 2020
So instead of using the max command is there another function that might give me the first or last peak per day. Giving me four outputs per day (two for surface chlorophyll surface and two for chlorophyll bottom)

Sign in to comment.


Peter Perkins
Peter Perkins on 20 Nov 2020
Not sure why you think a "loop is better". Try this:
>> t = table([1;1;1;1;2;2;2;2;2;2],rand(10,1),rand(10,1),'VariableNames',["x" "y" "z"])
t =
10×3 table
x y z
_ ________ _______
1 0.40286 0.67573
1 0.69316 0.2173
1 0.96076 0.91596
1 0.039119 0.75431
2 0.33803 0.5145
2 0.99243 0.87343
2 0.40484 0.90224
2 0.65143 0.22551
2 0.96835 0.27113
2 0.74077 0.83571
>> rowfun(@myfun,t,'GroupingVariable','x','OutputVariableNames',["ymax" "zmax"])
ans =
2×4 table
x GroupCount ymax zmax
_ __________ _______ _______
1 4 0.98035 0.13753
2 6 0.88784 0.45141
where
function [ymax,zmax] = myfun(y,z)
[ymax,imax] = max(y);
zmax = z(imax);
end
With your real data, same thing, just more verbose because if the long names:
t = readtable('DATA.csv');
rowfun(@myfun,t,'GroupingVariable','Date', ...
'OutputVariableNames',["time_bottom_max" "depth_chl_bottom_max" "Chl_Bottom_max" "time_surface_max" "depth_chl_surface_max" "Chl_Surface_max"])
function [time_bottom_max,depth_chl_bottom_max,Chl_Bottom_max,time_surface_max,depth_chl_surface_max,Chl_Surface_max] = myfun(time,depth_chl_bottom,Chl_Bottom,depth_chl_surface,Chl_Surface)
[Chl_Bottom_max,imax] = max(Chl_Bottom);
time_bottom_max = time(imax);
depth_chl_bottom_max = depth_chl_bottom(imax);
[Chl_Surface_max,imax] = max(Chl_Surface);
time_surface_max = time(imax);
depth_chl_surface_max = depth_chl_surface(imax);
end

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!