Concatenate Columns of Cells
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Antworten (3)
Jan Orwat
am 2 Jun. 2015
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 Kommentare
Jan Orwat
am 2 Jun. 2015
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
am 2 Jun. 2015
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 Kommentar
Jan Orwat
am 2 Jun. 2015
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
am 2 Jun. 2015
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 Kommentare
Stephen23
am 22 Dez. 2021
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.
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!