I've created a sliding window using the cell function. I used this as I am creating the sliding window for a daily time series of data over a few hundred years. The sliding window is 30 years long and because of leap years each row is a different length. So I now have an array of data that looks like this:
data =
10957x1 double
10957x1 double
10958x1 double
and so on...
I'm unfamiliar with the cell function however as this code was shown to me. How do I now use the data in this series? Whenever I type the command for example:
data (1,1)= 10957x1 double
But I need to do a number of things such as take the mean of each vector in the array, how do I do this?

 채택된 답변

Wayne King
Wayne King 2011년 10월 3일

0 개 추천

x = {randn(1000,1), randn(1000,1), randn(1000,1)};
meanz = cellfun(@mean,x,'UniformOutput',false);
meanz = cell2mat(meanz);

댓글 수: 5

Wayne King
Wayne King 2011년 10월 3일
you did say that these vectors are stored in a cell array, right?
James
James 2011년 10월 3일
I need to do a few more things than just take the mean. That was just an example.
I need to get the data from each cell into a usable format so I can say for example;
for 1:n
mean(data(n,1))
end
and
for 1:n
detrend(data(n,1))
end
James
James 2011년 10월 3일
Can you explain how to do this?
Wayne King
Wayne King 2011년 10월 3일
you can use cellfun to do that.
Jan
Jan 2011년 10월 3일
@James: Use curly braces to access cell elements:
for 1:n, mean(data{n,1}), end
for 1:n, detrend(data{n,1}), end

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

추가 답변 (5개)

Andrei Bobrov
Andrei Bobrov 2011년 10월 3일

0 개 추천

data2 = cellfun(@(x){mean(x) detrend(x)},x,'un',0)
ADD
data2 = cellfun(@(x)[mean(x);detrend(x)],x,'un',0)
datadouble = [data2{:}];
datamean = datadouble(1,:)
datadetrend = datadouble(2:end,:);

댓글 수: 1

James
James 2011년 10월 3일
This just gives me the same problem?
It still outputs it as a cell array, I cant do anything with a vector where all it shows me is:
1x2 double, for every row
How can I extract the data from this as it is not currently usable?
I need to plot graphs of this data using many different functions, not just the ones I've put on here.

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

Wayne King
Wayne King 2011년 10월 3일

0 개 추천

You can use two calls to cellfun and then use cell2mat
data1 = cellfun(@mean,x,'UniformOutput',false);
data1 = cell2mat(data1);
data2 = cellfun(@detrend,x,'UniformOutput',false);
data2 = cell2mat(data2);

댓글 수: 2

Wayne King
Wayne King 2011년 10월 3일
Notice that data2 contains the detrended data as column vectors, which you can plot()
plot(data2)
data1 is a row vector of means
James
James 2011년 10월 3일
Detrend outputs a new value for each value I have. So at the moment using cell2mat the function just seems to put all 30 year period into one single column. Is there a way to define where the data goes? I have tried this but it doesn't seem to work?
detrendcell = cellfun(@(T32)detrend(T32,'linear'),T32,'un',0);
% detrends the data in each cell
n = numel(detrendcell);
for i=1:n
detrendT(i) = cell2mat(detrendcell(i));
end

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

Fangjun Jiang
Fangjun Jiang 2011년 10월 3일

0 개 추천

Since data is a cell array, you need to use {} to reference its element, like data{1}, data{2}, data{3}, etc.
data{1} will be a 10957x1 double array, to reference its element, use data{1}(1), data{1}(100), data{1}(10957), etc.
James
James 2011년 10월 3일

0 개 추천

%%Data Loading
close all
clear all
cet = load ('cet_1772_2009.asc', '-ascii'); % Loads CET from asc file.
year = cet(:,1);
temp = cet(:,4);
day = cet(:,3);
month = cet(:,2);
% Loads Data from CET asc file and sets vectors
%%Constants and vectors
dates = datenum([year,month,day]);
%%30 Year Periods
StartYear=1772;
EndYear=2009;
Period=30;
T32=cell(EndYear-StartYear-Period+1,1);
for Year=StartYear:(EndYear-Period)
StartCount=datenum(Year,1,1)-datenum(StartYear,1,1)+1;
DataCount=datenum(Year+Period,12,31)-datenum(Year,12,31);
T32{Year-StartYear+1}=temp(StartCount:(StartCount+DataCount),1);
end
%%Detrend, mean etc
meancell = cellfun(@(T32)mean(T32),T32,'un',0);
meanT = cell2mat(meancell);
detrendcell = cellfun(@(T32)detrend(T32,'linear'),T32,'un',0);
n = numel(detrendcell);
for i=1:n
n1 = numel(detrendcell{i});
detrendT(:,i) = cell2mat(detrendcell{i}(1:n1));
end

댓글 수: 2

James
James 2011년 10월 3일
This is my code and it outputs the error:
??? Cell contents reference from a non-cell array object.
Error in ==> cell2mat at 43
cellclass = class(c{1});
Error in ==> Untitled2 at 63
detrendT(:,i) = cell2mat(detrendcell{i}(1:n1));
Any idea whats wrong with it?
Fangjun Jiang
Fangjun Jiang 2011년 10월 3일
Why need meancell = cellfun(@(T32)mean(T32),T32,'un',0)
Can it be meancell = cellfun(@mean,T32,'un',0)

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

Wayne King
Wayne King 2011년 10월 3일

0 개 추천

Why didn't you do what I suggested?
detrendcell = cellfun(@(T32)detrend(T32,'linear'),T32,'un',0);
detrendcell = cell2mat(detrendcell);
By the way, 'linear' is the default option for detrend:
so
detrendcell = cellfun(@detrend,T32,'UniformOutput',false);
detrendcell = cell2mat(detrendcell);
The above returns a matrix whose columns are your detrended series.
Is the same and a bit cleaner.

댓글 수: 5

James
James 2011년 10월 3일
Sorry I was unaware that the default was linear, the other option allowed me to set an optional arguement, thats why I chose it.
I have copy and pasted this exact code, it outputs detrendcell as a single column with 2279310 rows. It detrended each cell and then put them all on top of each other in one single column.
I need each cell detrended and then separately defined.
Wayne King
Wayne King 2011년 10월 3일
What was the size of your input T32?
James
James 2011년 10월 3일
It's a 208x1 cell array with each cell varying from 10957x1 to 10959x1... roughly estimating this 10958*208 = 2279264 so thats why I'm assuming these have all been arranged on top of each other in one column
Wayne King
Wayne King 2011년 10월 3일
Transpose your input
T32 = T32';
detrendcell = cellfun(@detrend,T32,'UniformOutput',false);
detrendcell = cell2mat(detrendcell);
James
James 2011년 10월 3일
This returns the error:
??? Error using ==> cat
CAT arguments dimensions are not consistent.
Error in ==> cell2mat at 77
m{n} = cat(2,c{n,:});
Error in ==> Untitled2 at 28
detrendcell = cell2mat(detrendcell)

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

카테고리

도움말 센터File Exchange에서 Data Type Identification에 대해 자세히 알아보기

질문:

2011년 10월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by