how to do logical indexing for, for and if loops to create a new dataset every time the condition is met.
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
arjun luther
le 25 Oct 2023
Réponse apportée : SAI SRUJAN
le 30 Oct 2023
Hi, i am trying to learn the loops in matlab and stuck in a situtaion. I have a time series dataset and i would like to do logical indexing of the table based on variable called global solar irradiance. Every time the value is between 180 and 400, the loops should create a new table based on whenever the condition is met. This would mean i should end up with around 30 to 40 or even more subsets/ tables . I have no idea where to begin. I know i would require a for and if loops for this .
I tried using simple logical indexing like
% refmean= 728
lower_threshold= 180 %0.01 * refmean
upper_threshold = 400 %0.80* refmean
highligting_indices=find(T3_filtered_final.("Global Solar Irradiance")>=lower_threshold & T3_filtered_final.("Global Solar Irradiance")<=upper_threshold)
New_Table=T3_filtered_final(highligting_indices,:)
This new table will have all the time intervals where the value of global solar irradiance meets the condition, but i would like to make subsets that whenever the conditions are met , the program should output a new table where the first value is always something near to 180 and last value near to 400. The new tables could have 1 second time gap or maybe 10 minutes time gap.
How to proceed with this program
for i =1:height(T3_filtered_final)% loop through whole table
if T3_filtered_final.("Global Solar Irradiance")(i)>=180 && T3_filtered_final.("Global Solar Irradiance")(i)<=400
% how to proceed
end
end
sample data is provided within.
9 commentaires
Réponse acceptée
SAI SRUJAN
le 30 Oct 2023
Hi Arjun luther,
I understand that you have a time series dataset in MATLAB and you want to create tables based on a condition involving the "Global Solar Irradiance" variable. Specifically, you want to create new tables whenever the value of "Global Solar Irradiance" is between 180 and 400.
You can follow the given example to proceed further.
% Set the lower and upper thresholds
lower_threshold = 180;
upper_threshold = 400;
% Initialize variables
subsetTables = {}; % Cell array to store subset tables
startIndex = 1; % Index to track the start of each subset - starting index
for i = 1:height(T3_filtered_final)
if T3_filtered_final.("Global Solar Irradiance")(i) >= lower_threshold && T3_filtered_final.("Global Solar Irradiance")(i) <= upper_threshold
% If the condition is met, update the start index
if startIndex == 0
startIndex = i;
end
else
% If the condition is not met and a subset has started,
% create a new table and insert it.
if startIndex ~= 0
endIndex = i - 1;
subsetTables{end+1} = T3_filtered_final(startIndex:endIndex, :);
startIndex = 0; % Reset the start index
end
end
end
% Check if a subset is still in progress at the end of the loop
if startIndex ~= 0
endIndex = height(T3_filtered_final);
subsetTables{end+1} = T3_filtered_final(startIndex:endIndex, :);
end
% You will end up with a cell array of tables
% which are created whenever conditon is met
% Access each subset table using subsetTables{index}
You can refer to the following documentation to understand more about "table" in MATLAB,
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!