How to identify an optimal subsample from a data set with missing values in MATLAB : <p>I would like to identify the largest possible contiguous subsample of a large data set. My data set consists of roughly 15,000 financial time series of up to 360 periods in length. I have imported the data into MATLAB as a 360 by 15,000 numerical matrix.</p>

<p><a href="https://i.stack.imgur.com/qOnzk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qOnzk.png" alt="enter image description here"></a></p>

<p>This matrix contains a lot of NaNs due to some of the financial data not being available for the entire period. In the illustration, NaN entries are shown in dark blue, and non-NaN entries appear in light blue. It is these light blue non-NaN entries which I would like to ideally combine into an optimal subsample.</p>

<p>I would like to find the largest possible contiguous block of data that is contained in my matrix, while ensuring that my matrix contains a sufficient number of periods.</p>

<p>In a first step I would like to sort my matrix from left to right in descending order by the number of non-NaN entries in each column, that is, I would like to sort by the vector obtained by entering <code>sum(~isnan(data),1)</code>.</p>

<p>In a second step I would like to find the sub-array of my data matrix that is at least 72 entries along the first dimension and is otherwise as large as possible, measured by the total number of entries.</p>

<p>What is the best way to implement this?</p>
