How to apply arrayfun for multiple column

9 views (last 30 days)
Dear all, I am using following code for the one column in a data matrix. however, I have 60 columns and 6892 rows in the data matrix. How to apply the collowing code on the all columns, please suggest.
n = 6892; % total number of rows
m = 100; % window size
h = arrayfun(@(k)genhurst(y(k:m+k-1)),1:30:(n-m+1),'UniformOutput',false); % 30 is the forward shifting size
HH=h.';

Accepted Answer

Guillaume
Guillaume on 28 Oct 2019
If you really want to use arrayfun you could do it like this:
windowsize = 100; %why call it m when window size is a lot clearer?
[rows, cols] = ndgrid(1:30:size(y, 1)-windowsize+1, 1:size(y, 2));
HH = arrayfun(@(row, col) genhurst(y(row:row+windowsize-1, col)), rows, cols, 'UniformOutput', false);
or you could just use an explicit loop which might be clearer and faster.
  7 Comments
Guillaume
Guillaume on 29 Oct 2019
I made a silly mistake in the explicit loop code. I forgot to subtract the window size from startrows. It should be:
startrows = 1:30:size(y, 1)-windowsize+1;
same as in the ndgrid code.
As for your later code. You have loaded the data into a variable called data but then use date which happens to be a matlab function. Note that as I've replied, if you want to apply genhurst by month, the easiest is with:
data = readtable('data.xlsx');
monthly_data = groupsummary(data, 'Date', 'month', @genhurst)
or with retime.
BISWA BHUYAN
BISWA BHUYAN on 29 Oct 2019
Edited: BISWA BHUYAN on 29 Oct 2019
I am sorry that i assume that i could not make you understand, may be due to lack of my clarification for my second query. Let me to explain again,
I applied the suggested code to a 3 column matrix without date column and it works very well
windowsize = 100;
startrows = 1:30:size(y, 1)-windowsize+1;
HH = cell(numel(startrows), size(y, 2));
for col = 1:size(y, 2)
for row = 1:numel(startrows)
HH{row, col} = genhurst(y(startrows(row):startrows(row)+windowsize-1, col));
end
end
Now, my second query was, i want to apply the above code where the equal number of date column will be in matrix. Then, I want to store the results with date in a new matrix, where the first window result will be store in the "windowsize+1" row with the corresponding date in a new matrix. For example, in the atatched data, the result of the first window of the x1 variable (assumed 0.25) and the second variable x2 (assumed 0.56) will be stored as follows,
[ 23-05-1994 0.25 24-08-1990 0.56]
Because, the "windowsize+1" row date of x1 is 23-05-1994 and the "windowsize+1" row date of x2 variable is 24-08-1990. Similarl procedure will be followed for all rolling windows. I have atatched the data file.
Please suggest
Coming to the suggestion of the monthly genhurst, i am reading your suggestion on groupsummary and retime. Then I will use code, if any problem i will let you know in future.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!