Concatenate Columns of Cells
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi,
I have asked this question earlier, but I think I have though of how to better phrase it.
Columns 1 through 5
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] '02/04/2004' [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] '03/04/2004' [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] '03/04/2004' [ 0] [ 0]
[ 0] [ 0] '03/04/2004' [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
There is only ever one date per row, and I want to concatenate them into one column, removing all the zeros.
I think it is difficult because of the cell/date format that I have going on. I've managed to make one very long vector of it all, and get rid of the zeros but lose the order they are in.
Any thoughts would be very much appreciated! Or any reading, or hints even...
Thanks!
답변 (3개)
Jan Orwat
2015년 6월 2일
Hello, I can see that date is of type string in your cell array and non-date fields are zeros (non-string). Assuming that in every row there is just one date you can get those like that:
c = yourCell.'; % rows becomes columns, columns becomes rows - this is for MATLAB indexing purposes
datesOnly = c(cellfun(@isstr,c));
This method will delete those rows which doesn't contain dates at all. It won't work properly if row contain more than one date because it gets all dates from array row by row.
댓글 수: 2
Jan Orwat
2015년 6월 2일
Hope this will help:
>> a = {
[ 0] [ 0] '20/7/2014'
[ 0] '23/7/2014' [ 0]
'26/7/2014' [ 0] [ 0]
[ 0] [ 0] [ 0]
'21/7/2014' [ 0] [ 0]};
>> [r,c] = find(cellfun(@isstr,a));
>> dates = accumarray(r,r+(c-1)*size(a,1),[],@(x)a(x),{'missing'})
dates =
'20/7/2014'
'23/7/2014'
'26/7/2014'
'missing'
'21/7/2014'
>> % or:
>> dates = accumarray(r,r+(c-1)*size(a,1),[],@(x)a(x),{0})
dates =
'20/7/2014'
'23/7/2014'
'26/7/2014'
[0]
'21/7/2014'
Thomas Koelen
2015년 6월 2일
I think this does what you want :)
a={'20/7/2014' 0 0; 0 '23/7/2014' 0; 0 0 '26/7/2014'};
j=1;
for i=1:size(a,1)*size(a,2)
if a{i}~=0
x(j)=i;
j=j+1;
end
end
for k=1:j-1
anew{k,1}=a{x(k)};
end
anew
a =
'20/7/2014' [ 0] [ 0]
[ 0] '23/7/2014' [ 0]
[ 0] [ 0] '26/7/2014'
anew =
'20/7/2014'
'23/7/2014'
'26/7/2014'
댓글 수: 1
Jan Orwat
2015년 6월 2일
Hi Thomas, in your example, when you change a:
>> a={0 0 '20/7/2014'; 0 '23/7/2014' 0; '26/7/2014' 0 0}
a =
[ 0] [ 0] '20/7/2014'
[ 0] '23/7/2014' [ 0]
'26/7/2014' [ 0] [ 0]
it gives:
>> anew
anew =
'26/7/2014'
'23/7/2014'
'20/7/2014'
so it looks like it does not work.
Andrei Bobrov
2015년 6월 2일
d = { '01/04/2004' [ 0] [ 0] [ 0] [ 0]
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] '02/04/2004' [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] '03/04/2004' [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] '03/04/2004' [ 0] [ 0]
[ 0] [ 0] '03/04/2004' [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'};
[ii,jj] = find( cellfun('ischar',d));
k = sortrows([ii,jj],1);
out = d(sub2ind(size(d),k(:,1),k(:,2)));
댓글 수: 2
Stephen23
2021년 12월 22일
The supported backward compatibility** options are described here:
and the correct syntax is:
cellfun('isclass',C,'char')
** It is worth nothing that these are more efficient than supplying a function handle.
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!