how to use cellfun to take relevant elements out of cell arrays

조회 수: 9(최근 30일)
Dear Friends,
I have a matrix of time series data,and I want to calculate the rolling zscore. Some columns start with NaNs, thus, I calculate zscore starting from the first non NaNs of each column, with a specficied rolling window, which will generate many cell arrays. finally, I want to take some elements out. specificaly,the cell arrays should have three types, the first type is NaN, the second type is the zscore calculated from the first non NAN to rolling window length, the last type is the rolling zscore. the rule is for the first type, it should be NaN, for the second type, it should be the ith element, for the third type, it should be the last element of each cell. but it seems I have incorrectly used cellfun, can anybody advise me how to proceed? thanks
TTMEYTemp=cell(TTMEYR,TTMEYC);
for i=1:TTMEYR;
for j=1:TTMEYC;
TTMEYInd(j)=min(find(~isnan(TTMEY(:,j))));
if i<TTMEYInd(j);
TTMEYTemp{i,j}=NaN;
TTMEYVec=NaN;
elseif i<TTMEYInd(j)+Rolling;
TTMEYTemp{i,j}=(TTMEY(TTMEYInd(j):TTMEYInd(j)+Rolling,j)-nanmean(TTMEY(TTMEYInd(j):TTMEYInd(j)+Rolling,j)))./nanstd(TTMEY(TTMEYInd(j):TTMEYInd(j)+Rolling,j));
TTMEYVec=cellfun(@(v) v(i),TTMEYTemp);%errors
else TTMEYTemp{i,j}=(TTMEY(TTMEYInd(j):i,j)-nanmean(TTMEY(TTMEYInd(j):i,j)))./nanstd(TTMEY(TTMEYInd(j):i,j));
TTMEYVec=cellfun(@(v) v(end),TTMEYTemp);%errors
end
end
end
% code
end
  댓글 수: 2
Peihong
Peihong 2013년 9월 25일
hi,Walter, the error message is
"Attempted to access v(2); index out of bounds because numel(v)=1.
Error in @(v)v(i)"

댓글을 달려면 로그인하십시오.

답변(2개)

Vishal Rane
Vishal Rane 2013년 9월 25일
편집: Vishal Rane 2013년 9월 25일
The function handle
@(v) v(i)
is trying to access the ith element of each cell contents of TTMEYTemp. This works for the first for loop iteration (i=1). Because
TTMEYTemp{1}(1)
is valid. But for i=2,
TTMEYTemp{1}(2)
is probably invalid, because each cell contents of your TTMEYTemp array is of size [1,1]
  댓글 수: 2
Vishal Rane
Vishal Rane 2013년 9월 25일
put a try-catch around your code to find the exact point at which error occurs

댓글을 달려면 로그인하십시오.


Jan
Jan 2013년 9월 25일
What is the intention of this line:
MEVec = cellfun(@(v) v(i), TTMEYTemp);
Seeing the failing code only does not allow to suggest an improvement, because we do not have a hint, what you expect as result.
Here cellfun provides each cell element to the anonymous function. And for any reasons, one of the elements is a scalar. But it is not efficient at all, to process the complete cell TTMEYTemp in each iteration, because only one element of this cell has changed since the last processing.
I find your code is very hard to read. For a better visual inspection I've replaced "TTME" by "e".
You can use the debugger to inspect your problem:
dbstop if error
Then run the code again until it stops at the error. Now you can check the local variables.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by