2

I need to subtract months from a given DateCol in a matrix. I was using the command bsxfun(@minus, datevec(date),[0,3,0,0,0,0]) to subtract 3 months from the given date. You can see there is a bug in this code. If the date is 1-Feb-2011, this code takes you to 1-Jan-2011 instead and NOT 1-Nov-2010.

mat = [100 734505 ; 200 734536 ; 200 734539] ; % Col1 is ID and Col2 is Date

Can you suggest a vectorized approach? For a single-date (outside of matrix), addtodate(d, -3, 'month') works perfectly fine!

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Maddy
  • 2,520
  • 14
  • 44
  • 64

1 Answers1

1

how about this?

newDateVector = arrayfun(@(x) addtodate(x, -3, 'month'), oldDateVector)
foglerit
  • 7,792
  • 8
  • 44
  • 64