reformatting a matrix in matlab with nan values

Posted by Kate on Stack Overflow See other posts from Stack Overflow or by Kate
Published on 2012-10-26T10:42:07Z Indexed on 2012/10/26 11:01 UTC
Read the original article Hit count: 147

Filed under:
|

This post follows a previous question regarding the restructuring of a matrix:

re-formatting a matrix in matlab

An additional problem I face is demonstrated by the following example:

depth = [0:1:20]';
data = rand(1,length(depth))';
d = [depth,data];
d = [d;d(1:20,:);d];

Here I would like to alter this matrix so that each column represents a specific depth and each row represents time, so eventually I will have 3 rows (i.e. days) and 21 columns (i.e. measurement at each depth). However, we cannot reshape this because the number of measurements for a given day are not the same i.e. some are missing. This is known by:

dd = sortrows(d,1);
for i = 1:length(depth);
    e(i) = length(dd(dd(:,1)==depth(i),:));
end

From 'e' we find that the number of depth is different for different days. How could I insert a nan into the matrix so that each day has the same depth values? I could find the unique depths first by:

unique(d(:,1)) From this, if a depth (from unique) is missing for a given day I would like to insert the depth to the correct position and insert a nan into the respective location in the column of data. How can this be achieved?

© Stack Overflow or respective owner

Related posts about matlab

Related posts about matrix