f



How avoid a loop for in a table?

Hi everyone,

I have a very big table called "export" (more than 2000000 rows) with a column of dates called "expiration" and I have this part of code:

for i 1:size(export,1)
if weekday(export.Expiration(i,1))==7 
       export.Expiration(i,1) = cellstr(datetime(datenum(export.Expiration(i,1))-1,'ConvertFrom','datenum','Format','MM/dd/yyyy'));
end
end

My problem is that the loop for is very slow. Anyone knows how to avoid the loop for to get the same result? thanks a lot!!
0
Adriano
12/22/2016 9:06:03 AM
comp.soft-sys.matlab 211266 articles. 13 followers. lunamoonmoon (258) is leader. Post Follow

1 Replies
16 Views

Similar Articles

[PageSpeed] 48

On 12/22/2016 3:06 AM, Adriano wrote:
> Hi everyone,
>
> I have a very big table called "export" (more than 2000000 rows) with a
> column of dates called "expiration" and I have this part of code:
>
> for i 1:size(export,1)
> if weekday(export.Expiration(i,1))==7
 >
  export.Expiration(i,1) =
> cellstr(datetime(datenum(export.Expiration(i,1))-1,'ConvertFrom','datenum','Format','MM/dd/yyyy'));
>
> end
> end
>
> My problem is that the loop for is very slow. Anyone knows how to avoid
> the loop for to get the same result? thanks a lot!!

ix=(weekday(export.Expiration(i,1))==7);
export.Expiration(i,1)=cellstr(datetime(datenum(export.Expiration(ix,1))-1,...
                        'ConvertFrom','datenum','Format','MM/dd/yyyy'));

But why convert the actual variable to string at all, why not just 
display as desired from the base value if use the datetime class 
instead?  And, even more why are you mixing the two???
0
dpb
12/22/2016 1:40:49 PM
Reply: