filling a cell array with pairs of columns of different length in a loop where one column remains fixed (but may vary in size) and the other changes with the loop

16 views (last 30 days)
Dear All,
I’m stuck with something intractable. I have some raw time series, where the rows of NaNs at the start and end of the series vary. The stuff I need to do with this data only works for series not containing NaNs. The trouble is, the statistics I need are to be obtained for combinations of series. So if my data contains, say, 3 columns, then I want column 1 and 2, then column 1 and 3, and I have to store them (so I can do stuff with them later). Because the rows with NaN value at the end and beginning of the three column varies, the combination of column 1 and column 2 will be of different size from the combination of column 1 with 3. The way to do this, I believe, is to use a cell array, which caters for something like that. So if my code is:
[n,l] = size(data); %find length of data (raw, including NaN rows)
B = cell(nCols-1,1); %preallocate a cell to store matrices of different row length
for ii= 1:nCols
%loop to pair match-wise rows without adjacent NaNs (two columns at a time)
for i=1:n;
if sum(isnan(data(i,:)))>=1
i=i+1;
else
break
end
end
B{ii}=data(i:n,:);
end
The inner loop (from the second ‘for’ to the second last ‘end’)works (I tried it on two columns of data, it does the job-it truncates the sample (for both) at the first row where both vectors contain data.
The column index in the RHS of the second last line is obviously wrong-what I would like matlab to do is to pick column 1 and column 2, truncate them at the appropriate length (starting with the 1st adjacent pair of rows not containing NaNs, and storing them in the cell, then for ii = 2, I would like matlab to take column 1 and column 3, do the same operation, and stick the resulting pair of columns in the next cell, and so on (here, for expositional clarity there’s only 3 time series, in reality there’s a large number of them.
What to do? I’m at a loss.
Many thanks in advance.

Answers (1)

Prabhan Purwar
Prabhan Purwar on 27 Jul 2021
Hi,
I will suggest concatenating the vectors obtained using NaN to avoid dimension mismatch and later remove NaN using rmmissing. rmmissing functionality is used to remove the missing entries from the matrix or tables similar to the example mentioned above.
Hope it helps!!

Tags

Community Treasure Hunt

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

Start Hunting!