Find a value which is on the same index as mulitple different values in a matrix

2 Ansichten (letzte 30 Tage)
I have a 3608x5 matrix which is year-month-date-time-energy at respective columns, where there are multiple datapoints every day. I have extracted the maximal values for one day manually placing the limits but want to extract the time of these values without getting a value from another day.
In this example I'm trying to get the timestamps in 8th of august but it doesn't work.
matrix=[year,month,date,time,energy]
max_day= [max energy consumption during a day]
for i=1:1:size(max_day)
time(i)=matrix(find(matrix(:,5)==max_day(i) & matrix(:,2)==8),4)
end

Antworten (1)

Himanshu
Himanshu am 23 Feb. 2024
To my understanding you are trying to extract the value of time column for a particular day (8th august), corresponding to the maximum value of the ‘energy’ column on that day.
In the code shared by you, the issue lies in how you're filtering the data based on the date.
for i=1:1:size(max_day)
time(i)=matrix(find(matrix(:,5)==max_day(i) & matrix(:,2)==8),4)
end
  • matrix(:,5)==max_day(i) is comparing the energy values in your matrix with the maximal energy consumption for each day (max_day). This part of the condition is correct.
  • matrix(:,2)==8 is intended to filter the data for the 8th month (August). However, it doesn't consider the specific date. It checks if the month column in your matrix is equal to 8, but it doesn't ensure that the date corresponds to the 8th of August.
To filter the data for the 8th of August, you need to check both the month and date columns. In your original code, you only checked the month column matrix(:,2)==8, which is why it didn't correctly filter the data for the 8th of August.
Add a condition to check for the date in your code to achieve the desired result.
Hope this helps!

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by