Organizing dates and values using simple matrices
조회 수: 1 (최근 30일)
이전 댓글 표시
I have data of that look as that looks like this:
Year Month Day1 Day2 Day3 ... Day31
1999 01 VAL VAL VAL VAL
1999 02 VAL VAL VAL VAL
It's a csv file and VAL is a real number. I'm using a mix of textread and csvread to open them (depending, some of the files have headers that need to be included) and it seems like a lot of matrix rearranging. I think I have an idea of how to use MATLAB to organize them into cell arrays, but actually cell arrays won't help me in the end. What is needed is just a x-by-2 matrix that basically is like
1999-01-01 VALUE
1999-01-02 VALUE
1999-01-03 VALUE
1999-01-04 VALUE
I'm mainly not sure if the first column is possible like that or if it needs to be either a string or a x-by-4 matrix like this
1999 01 03 VAL
Thanks for any suggestions.
댓글 수: 2
답변 (1개)
Azzi Abdelmalek
2015년 7월 25일
편집: Azzi Abdelmalek
2015년 7월 26일
Edit
%-----------------Example------------------------
A=[ {'year' 'month'} genvarname(repmat({'day'},1,31),'day');repmat({1999},12,1) num2cell((1:12)') num2cell(rand(12,31))]
%----------------The code------------------------
B=A(2:end,3:end);
BB=B(:);
y=repmat(A(2:end,1),1,31)';
y=y(:);
m=repmat(A(2:end,2),1,31)';
m=m(:);
d=num2cell(repmat((1:31)',size(B,1),1));
out=[y m d BB];
idx=cellfun(@isempty,out(:,4)); %-----------Edited line--------------
out(idx,:)=[]
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!