How can I use a for loop to interpolate to a certain number of points, and remove NaNs?
3 views (last 30 days)
Show older comments
I have a for-loop to remove NaN values at the end of my dataset, and this currently interpolates to the longest trial in my dataset:
[Ny,Nx] = size(sr_norm);
Nz = sum(~isnan(sr_norm));
for k = 1:Nx
sr_interp(:,k) = interp1(1:Nz(k),sr_norm(1:Nz(k),k),1:Ny,'linear','extrap'); %Remove NaN at the end
end
sr_transpose=sr_interp';
I am trying to interpolate the data set to 360 points, so that for every 1 that pr_transpose increases, we can see what the increase in sr_transpose is. Each dataset is longer than 360 points. I have been able to do this after removing the NaN values by repeating for each trial number as below:
xq=(0:1:360);
p1=pr_transpose1(1,:);s1=sr_transpose(1,:);sr1=interp1(p1,s1,xq);
p2=pr_transpose1(2,:);s2=sr_transpose(2,:);sr2=interp1(p2,s2,xq);
p3=pr_transpose1(3,:);s3=sr_transpose(3,:);sr3=interp1(p3,s3,xq);
p4=pr_transpose1(4,:);s4=sr_transpose(4,:);sr4=interp1(p4,s4,xq);
p5=pr_transpose1(5,:);s5=sr_transpose(5,:);sr5=interp1(p5,s5,xq);
ParticipantNo_1_S = [sr1; sr2; sr3; sr4; sr5];
This I know is really clunky, but I have not been able to figure out how to incorporate them together. Any thoughts and ideas would be much appreciated!
2 Comments
Rik
on 2 May 2023
What exactly is your question? Do you want to know hoew to avoid numbered variables? Or do you want to know how to select the first 360 non-NaN values in each row?
In case it is the latter; is there a guarantee that there will always be 360 such values?
Accepted Answer
Rik
on 2 May 2023
Edited: Rik
on 2 May 2023
For the second part it is easy enough with a few cell vectors as an intermediate step (if you want to store the separate elements). I'm not sure I understand the first section well enough to suggest how to merge the two sections.
xq=0:360;
for n=1:size(pr_transpose1,1)
p{n}=pr_transpose1(n,:);
s{n}=sr_transpose(n,:);
sr{n}=interp1(p{n},s{n},xq);
end
ParticipantData = vertcat(sr{:});
0 Comments
More Answers (1)
dpb
on 2 May 2023
Edited: dpb
on 2 May 2023
Like @Rik, I don't understand the desire well enough to know precisely the result you're looking for, but to find the locations of the NaN in the original array and interpolate it to the range 0:360 based on length of each finite segment in each column is straightforward enough with the help of arrayfun to walk through the columns of the array. Explicit for loops can be used as well, of course, this done here for pedagogical reasons as much as anything, just to illustrate...
load sample_data
whos % see what is in the .mat file
iz=arrayfun(@(c)find(isfinite(sr_norm(:,c)),1,'last'),1:size(sr_norm,2),'uni',1) % find the end finite location
xq=[0:360].'; % the interpolated points (NB: is 361 points, not 360)
p=cell2mat(arrayfun(@(r,c)interp1([0:r-1].',sr_norm(1:r,c),xq),iz,1:size(sr_norm,2),'uni',0));
size(p)
[p(1:5,:);p(357:end,:)] % show first, last few columns result
The interpolation goes from 0:N-1 to avoid the initial NaN and need for the 'extrap' flag; then just use the row lengths determined from the find operation. Using isnan there returns the location past the last finite location; you can use that but then the one column (third, I think it is) is full length and returns empty for that column; that then takes more code to fixup that location. Finding the last finite location in each column instead keeps everything bounded.
As noted, I don't really follow what the actual end result wanted here is, but that should give pointers on at least one way to do the interpolation. Another choice if happen to have the Signal Processing TB would be resample although there are also versions for the timeseries which won't help much here without a time vector although one could create an artificial one. I've found the timeseries of limited use although I'm sure there are places/applications it would work out just fine; it's never seemed to fit the specific things I've ever tried to do when I have poked at it...
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!