How to apply arrayfun for multiple column
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
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.';
채택된 답변
Guillaume
2019년 10월 28일
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
BISWA BHUYAN
2019년 10월 28일
Woww...this is very nice. But, please give an example of explicity loop. I do not know what is this?
windowsize = 100;
startrows = 1:30:size(y, 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
BISWA BHUYAN
2019년 10월 28일
Thank you very much. I have one more similar problem, but with a different manner. I have atatched my data set. In the data set six variables are there (from x1 to x6) and one date column. The date ranges from 03-01-1994 to 29-12-1995. It is in date-month-year format.
Now i want to use function 'genhurst' on the each month in each year. This will be used for all variables.
To be more specific, in the first step data will be split on the basis of month and year. So, we will get 12 months in a year. In the second step, 'genhurst' will be used for each month and year. In the third step, this will be performed for all variables. If my question is not clear, please ask me, I will explain.
Please suggest.
data = readtable('data.xlsx');
monthly_data = groupsummary(data, 'Date', 'month', @genhurst)
or
data = readtimetable('data.xlsx');
monthly_data = retime(data, 'monthly', @genhurst)
BISWA BHUYAN
2019년 10월 29일
편집: BISWA BHUYAN
2019년 10월 29일
Query on above suggestion because i got error in the above suggestion. When i used your suggestion,
>> windowsize = 100;
startrows = 1:30:size(y, 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
I got the following error.
Index exceeds matrix dimensions.
However, when i used below code, i am not getting any error.
windowsize = 100;
[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);
The above code is performing without date column. However, when there is 'm' number of date column with 'm' number of variable available, then the above code is not working. For example, the atatched excel file contains two date column and two variables column. When the following code applies, I am getting errors.
data = readtable('data.xlsx');
windowsize = 100;
startrows = 1:30:size(date, 1);
HH = cell(numel(startrows), size(date, 1));
for col = 1:size(date, 1)
for row = 1:numel(startrows)
HH{row, col} = genhurst(date(startrows(row):startrows(row)+windowsize-1, col));
end
end
Error using date
Too many input arguments.
In the above code i want to store the outcome of first window on the date row of 'windowsize+1' along with the respective date in a new matrix.
Please suggest
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
2019년 10월 29일
편집: BISWA BHUYAN
2019년 10월 29일
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.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
