Concatenate Columns of Cells

4 次查看(过去 30 天)
Chameleon17
Chameleon17 2015-6-2
评论: Stephen23 2021-12-22
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
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 个评论
Chameleon17
Chameleon17 2015-6-2
Hi!
Thank you a lot! This worked! I've been stuck here for so long.
I have come up with a different problem now. I have realized that not every row has a date... but I need them to maintain their original order, with a space if the date is missing. Do you know if it would be possible to do this?
Jan Orwat
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
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
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
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 个评论
Chameleon17
Chameleon17 2015-6-2
Hi, I would like to use your solution. Your solutions have been very helpful! This keeps telling me that there is 'Error using cellfun Unknown option.' I'm not sure how to fix that unfortunately...
Stephen23
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by