how to use cellfun to take relevant elements out of cell arrays
조회 수: 9(최근 30일)
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
Vishal Rane 2013년 9월 25일
편집: Vishal Rane 2013년 9월 25일
The function handle
is trying to access the ith element of each cell contents of TTMEYTemp. This works for the first for loop iteration (i=1). Because
is valid. But for i=2,
is probably invalid, because each cell contents of your TTMEYTemp array is of size [1,1]
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.