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

4 views (last 30 days)
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 Comments
Peihong
Peihong on 25 Sep 2013
hi,Walter, the error message is
"Attempted to access v(2); index out of bounds because numel(v)=1.
Error in @(v)v(i)"

Sign in to comment.

Answers (2)

Vishal Rane
Vishal Rane on 25 Sep 2013
Edited: Vishal Rane on 25 Sep 2013
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 Comments
Peihong
Peihong on 25 Sep 2013
hi, Vishal,each cell contents in TTMEYTemp will either be NaN (for the first few lines containing missing data) or a colum vector, with length larger than 40.
Vishal Rane
Vishal Rane on 25 Sep 2013
put a try-catch around your code to find the exact point at which error occurs

Sign in to comment.


Jan
Jan on 25 Sep 2013
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.

Categories

Find more on Categorical Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!