How to interpolate based on the first column in a matrix and fill the interpolated rows of the other columns with set values?

2 views (last 30 days)
Hello,
I am trying to interpolate between values in column 1 of an nX8 matrix. I want the interpolation step to be constant (e.g. step size = 1). I want to start at 0 and go up to the first value in column 1, then add the correct values in between successive rows. I want to insert these interpolated values into the original nX8 matrix, but I want to specify values for the rest of the inserted row (i.e. the remaining 7 elements in the row) to be a mixture of zeros or NaNs (it would be the same for every row). Here is an example of what I am trying to do but with fewer columns:
example = [3.0000 0.9172 0.3804 0.5308 0.5688
5.0000 0.2858 0.5678 0.7792 0.4694
7.0000 0.7572 0.0759 0.9340 0.0119
10.0000 0.7537 0.0540 0.1299 0.3371];
%...Code to do the job...
result = [0.0000 NaN NaN 0 NaN
1.0000 NaN NaN 0 NaN
2.0000 NaN NaN 0 NaN
3.0000 0.9172 0.3804 0.5308 0.5688
4.0000 NaN NaN 0 NaN
5.0000 0.2858 0.5678 0.7792 0.4694
6.0000 NaN NaN 0 NaN
7.0000 0.7572 0.0759 0.9340 0.0119
8.0000 NaN NaN 0 NaN
9.0000 NaN NaN 0 NaN
10.0000 0.7537 0.0540 0.1299 0.3371];
Thanks!

Accepted Answer

Matt J
Matt J on 12 Nov 2021
Edited: Matt J on 12 Nov 2021
example = [3.0000 0.9172 0.3804 0.5308 0.5688
5.0000 0.2858 0.5678 0.7792 0.4694
7.0000 0.7572 0.0759 0.9340 0.0119
10.0000 0.7537 0.0540 0.1299 0.3371];
step=1;
fill=[ NaN NaN 0 NaN];
first=( 0:step:max(example(:,1)) ).';
n=numel(first);
result=[first, repmat(fill,n,1)];
[tf,loc]=ismembertol(example(:,1),first);
result(loc(tf),:)=example(tf,:)
result = 11×5
0 NaN NaN 0 NaN 1.0000 NaN NaN 0 NaN 2.0000 NaN NaN 0 NaN 3.0000 0.9172 0.3804 0.5308 0.5688 4.0000 NaN NaN 0 NaN 5.0000 0.2858 0.5678 0.7792 0.4694 6.0000 NaN NaN 0 NaN 7.0000 0.7572 0.0759 0.9340 0.0119 8.0000 NaN NaN 0 NaN 9.0000 NaN NaN 0 NaN

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!