How Can I avoid a loop for in a table?

5 ビュー (過去 30 日間)
Adriano
Adriano 2016 年 12 月 22 日
コメント済み: KSSV 2016 年 12 月 23 日
Hi everyone,
I have a table called 'export' with more than 2000000 rows. One of its column, called ' Expiration', is a dates vector. I woul like to avoid the loop for in this 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
Can somebody help me? Many thanks.
  3 件のコメント
Shadi Mahdiani
Shadi Mahdiani 2016 年 12 月 22 日
If you can provide a few rows of your table, it would be easier to find out what you are exactly looking for. But generally find and strncmp help when you struggle with big tables and want to avoid for loops.
Guillaume
Guillaume 2016 年 12 月 22 日
Indeed an example would have made things clearer, however the format can be inferred from the given code. export.Expiration is a column of date strings in the form 'MM/dd/yyyy'
The purpose of the loop is to change the date to the day before when it is the last day of the week.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2016 年 12 月 22 日
編集済み: Guillaume 2016 年 12 月 22 日
An example of the data and an explanation of what you want to achieve would indeed have been useful. From your code, it would appear that the dates are stored as date strings and that if the date is the last day of the week, you want to make it the day before.
That is trivial to achieve. The exact equivalent of your code without a loop is simply:
wday = weekday(export.Expiration);
export.Expiration(wday == 7) = cellstr(datetime(datenum(export.Expiration(wday == 7) - 1, 'ConvertFrom', 'datenum', 'Format', 'MM/dd/yyyy'));
However, I would recommend that you actually store your date as datetime, which would make the date manipulation much easier and avoid this triple conversion to date number -> datetime -> date string:
export.Expiration = datetime(export.Expiration, 'InputFormat', 'MM/dd/yyyy', 'Format', 'MM/dd/yyyy');
islastday = day(export.Expiration, 'dayofweek') == 7;
export.Expiration(islastday) = export.Expiration(islastday) - 1;
I assume you're aware that in matlab, the last day of the week is Saturday. Otherwise you'll have to change the 7 to 1, if you meant to find the Sundays.
  1 件のコメント
Peter Perkins
Peter Perkins 2016 年 12 月 22 日
One small tweak:
export.Expiration(islastday) = export.Expiration(islastday) - caldays(1);
With no time zone, it won't matter, but it is more readable, and it will continue work correctly if time zones are added and the day happens to fall on a Daylight Saving shift.
Another possibility would be
export.Expiration(islastday) = dateshift(export.Expiration(islastday),'dayofweek','saturday','previous');

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2016 年 12 月 22 日
You can convert your table into cell using table2cell and access complete row/column you want.
  2 件のコメント
Guillaume
Guillaume 2016 年 12 月 22 日
You can access complete rows/columns of a table directly with indexing or dot notation, there's no need to waste time converting them to anything.
KSSV
KSSV 2016 年 12 月 23 日
That's true....but if any head lines of the table present, they do come along with row/ column. Isn't it?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by