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!
1 件のコメント
回答 (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!