Inserting data into matrices where gaps exist.
10 次查看(过去 30 天)
显示 更早的评论
I have a matrix that will be different depending on the data set I'm working with. It will always have a few columns where the first column is a reference column which starts are some arbitrary (but meaningful) value and increases by 1 for each row. The set of data will almost always be missing values and I can find the location of these missing points by building a perfect reference column and comparing it to the original data set (see below).
ref = [data(1):data(end)]' %rebuilding the reference column which will be equal to or larger than the original data set.
gaps = find(~ismember(ref, data(:,1))) %locates missing values in the original data set, based on the reference column.
From here I am trying to find an elegant solution to filling in the missing data by inserting rows. I've looked at close to 15 solutions so far and none really work for what I am trying to do. Hope someone can help before I finish option 2 (which sounds like a lot of work to me).
Option 1: I'm hoping there is an easy way to insert the missing reference values and NaNs into the desired matrix rows, before use fillmissing() to finish the job, but I don't know if this is even possible.
Options 2: Split the matrix at an identified gap, build a new matrix by appending a bottom row to the top half of the original matrix, and then using vertcat() to rebuild the matrix. Repeat for each instance until no gaps are identified. I guess this would need a while loop.
I am using R2017a.
0 个评论
采纳的回答
Cris LaPierre
2020-12-4
We don't have much to work with, but perhaps you can join your tables? If you want to try this interactively, use the Join Table task in the live editor.
3 个评论
Cris LaPierre
2020-12-4
The details matter because it ensures we provide a solution that works for you. Here is how I might solve the problem based on the sample data given.
data = [1 1; 2 3; 3 5; 5 7; 6 9; 7 10; 9 11; 10 10];
% Define the desired data points and interpolate the missing values
xq = 1:10;
yq = interp1(data(:,1),data(:,2),1:10);
newData = [xq; yq]'
Note that this is slightly different from your data_fixed, but I'm assuming there is a mistake in filling the gap between 3 and 5.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!