Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Update including leap years</h2> <p>I doubt it matters for the amount of data you have, but I think I came up with a solution including leap years and not using any loops. Thus, for the sake of completeness. We will build the idea up again with a mini-example where a leap year has 11 days, a normal year 10.</p> <pre><code>data = 1:103 isLeapYear = [false, false, true, false, false, true, false, false, true, false] </code></pre> <p>That vector you should replace with a result of the isLeapYear-function for all years. </p> <p>Next, we generate a matrix containing 11 (or, in real life 366) rows and the number of years columns, containing only the value 1:</p> <pre><code>helpmatrix = ones(11, 10) </code></pre> <p>and set the last row of the matrix to 0 if the corresponding year is not a leap year:</p> <pre><code>helpmatrix(end, ~isLeapYear) = 0 </code></pre> <p>Let's write the matrix as a vector (using reshape as described below) and sum up all the ones</p> <pre><code>selector = cumsum(reshape(helpmatrix, prod(size(helpmatrix)), 1)) </code></pre> <p>Those values we can use to blow up our original data in a way that each year has 11 (366) days and we could use the trick below. To fill up the value of day 11 we just reuse the value of day 10, this does neither change the maximum not the minimum value. (But be aware: Other functions might as well be affected by this!)</p> <pre><code>max(reshape(data(selector), 11, length(selector)/11)) </code></pre> <p>To transform this you would just have to exchange all the 10 and 11's by 365 and 366 and to alter the last command using <code>data(selector, 2)</code> instead of <code>data(selector)</code>.</p> <p>Probably this is not much of a gain compared to the loop solution, but it shows the power of vector computations.</p> <hr> <h2>Old suggestion</h2> <p>You can do that on a much simpler basis.</p> <p>The function <code>reshape</code> allows you to transform a vector (or a matrix, if necessary) into another row/column-layout.</p> <p>Your problem then could be solved by</p> <pre><code>max(reshape(data(:,2), 365, length(data)/365)) </code></pre> <p>To understand what is happening I created a smaller example:</p> <pre><code>data = 1:100 reshape(data, 10, length(data)/10) max(reshape(data', 10, length(data')/10)) </code></pre> <p>Your loop above would also work, but inefficient. You would have to take care of how your indices are created. You do <code>i:365*i</code>. In my example above this looks like</p> <pre><code>for i=1:10 i:10*i end </code></pre> <p>which is clearly not what you want. Done the right way you could do</p> <pre><code>for i=1:10 (10*(i-1) + 1):10*i end </code></pre> <p>Hope that helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload