MATLAB: Loop with two varying conditions

loop two variables date value vector

Hello
I would like to have some help with the following (probably simple but has me stumped) problem.
I have a matrix [35040 x 2] consisting of a datenumber series in one column and corresponding values. I also have a vector [12 x 1] values.
I am trying to add each value from the vector to all values in each corresponding month in the matrix. Like this:
(lets call this a month)
735235.010416667 4.36000000000000
735235.020833333 4.44000000000000
735235.031250000 4.43500000000000
735235.041666667 4.41000000000000
735235.052083333 4.37000000000000
735235.062500000 4.35000000000000
735235.072916667 4.35000000000000
Add value vector(1,1) to the above. Then repeat this by adding value vector(2,1) to the values of month 2 from matrix.
Any help really appreciated. Thanks !

Best Answer

The most straightforward approach is probably to build a vector of month IDs, and then to loop over months and update values. Something along these lines (assuming that your 35040x2 array is named data):
[~, monthId] = datevec( data(:,1) ) ;
for m = 1 : 12
data(monthId==m,2) = data(monthId==m,2) + vector(m) ;
end
You can make it more concise and eliminate the loop as follows (but it is a bit less easy to understand):
[~, monthId] = datevec( data(:,1) ) ;
data(:,2) = data(:,2) + vector(monthId) ;