MATLAB Answers

0

Reshaping 2d array

Asked by venkat siddhartha rama on 20 Oct 2019
Latest activity Edited by Andrei Bobrov
on 22 Oct 2019
greetings friends,
Problem Definition:
I had a variable named Interpreted_power_1st_column = 105120 X 1 ;
  • This is the power availablitiy data at a location collected at every 5 mins interval for whole year.( 8760*12=105120) (there are 12 5 mins intervals in 1 hour ,and 8760 hours in 365 days)
  • I want the data to be avaerged to 1 hour interval, so I used reshape function in matlab( code below) to sum the 12 consequtive data points multiply by 5 and then divide the whole summation by 60....this is actually good way to get average data for 1 hour interval.
hourly_wind_energy1=zeros(8750,1);
hourly_wind_energy1=((sum(reshape(Interpreted_power*5,12,8760)))/60); %Hourly Wind energy generated from intepretation of power curve KW-hr
hourly_wind_energy1=hourly_wind_energy1';%%Hourly Wind energy generated from intepretation of power curve KW-hr
Now I have to do the same but Interpreted_power = 105120 X 5 (5 columns)
I have be trying various ways like for loops but couldnot achieve my final result.
Possible solution 1 : any experts who are well aware of reshape function help me with a possible way to do the process I have done in problem definityin for an variable with several columns (for now 105120 X 5, 5 columns)
Possible solution 2:
  • to create 5 different variables (Interpreted_power_1st_column, Interpreted_power_2nd_column, Interpreted_power_3rd_column, Interpreted_power_4th_column, Interpreted_power_5th_column)
  • Interpreted_power_1st_column has 1st column of Interpreted_power = 105120 X 5
  • Interpreted_power_2nd_column has 2nd column of Interpreted_power = 105120 X 5
  • Interpreted_power_3rd_column has 3rd column of Interpreted_power = 105120 X 5
  • Interpreted_power_4th_column has 4th column of Interpreted_power = 105120 X 5
  • Interpreted_power_5th_column has 5th column of Interpreted_power = 105120 X 5
Now follow the same procedure used in Problem definition ( Use reshape function 5 times for 5 of these variables, then combine all the results into single variable with 8760 X 5 )
Problem with solution 2 : I am having trouble creating individual column variables,
Interpreted_power_1st_column=Interpreted_power(1:end);
Interpreted_power_1st_column=Interpreted_power_1st_column'; %% this is giving 1st column elements
Interpreted_power_2st_column=Interpreted_power(2:end);
Interpreted_power_2st_column=Interpreted_power_2st_column'; %% but all below are not exactly the required column elements
Interpreted_power_3st_column=Interpreted_power(3:end);
Interpreted_power_3st_column=Interpreted_power_3st_column';
Interpreted_power_4st_column=Interpreted_power(4:end);
Interpreted_power_4st_column=Interpreted_power_4st_column';
Interpreted_power_5st_column=Interpreted_power(5:end);
Interpreted_power_5st_column=Interpreted_power_5st_column';
Thanks in advance

  4 Comments

Show 1 older comment
My matrix with (105120 x1) has data of power available at a single place within every 5 mins interval. In the code that I used, I used reshape function to get the average of the power with in the 1 hour (i.e sum of 12 consequtive powers myltiplied by 5 that are available wuth 5 mins interval and divided by 60) ( this is an good estimate of hourly average power available)... Now i have to do the same for interpreted power matrix, but instead of (105120x1) i have (105120x5) i.e this has the power available with in 5 mins interval of 5 places. Each column represents separate location of available power. My final goal is to plot the 5 columns in a single graph against index at X axis.
I hope I explained it to the extent. Please feel free to ask anything that I missed.
David Hill on 22 Oct 2019
Since you have an algorithm for (105120x1), just pass all columns into the function.
secondCol=powerMatrix(:,2);
thirdCol=powerMatrix(:,3);
I don't know which release you're on but starting from 2016b, if you had your data stored in a timetable you could simply call retime:
Thourly = retime(T,'hourly','mean')

Sign in to comment.

Tags

1 Answer

Andrei Bobrov
Answer by Andrei Bobrov
on 22 Oct 2019
Edited by Andrei Bobrov
on 22 Oct 2019
 Accepted Answer

As in comment by Sebastian Bomberg's (for MATLAB >= R2016b):
% Let A - your data of energy (150120 x 5 double)
TT = array2timetable(A,'RowTimes',(datetime(2018,1,1,0,0,0):minutes(5):datetime(2018,12,31,23,55,0))');
TT_hourly_mean = retime(TT,'hourly','mean');
if MATLAB <= R2016a:
[y,m,d,h,~,~] = datevec((datetime(2018,1,1,0,0,0):minutes(5):datetime(2018,12,31,23,55,0))');
T = [table(y,m,d,h),array2table(A)];
T_hourly_mean = varfun(@mean,T,'GroupingVariable',{'y','m','d','h'});

  0 Comments

Sign in to comment.