replace all values of cells between two positions

I have a matrix and vectors of noise onsets and correspondig offsets positions for each row in the matrix.
I need to replace all the cell values between onsets and offsets with NaN's. For example:
matrix =
29.6676 48.8609 67.9136 33.5357 89.0923 61.7666 97.8681 81.8149 43.2392 52.6876
31.8778 57.8525 39.5515 67.9728 33.4163 85.9442 71.2694 81.7547 82.5314 41.6799
42.4167 23.7284 36.7437 13.6553 69.8746 80.5489 50.0472 72.2440 8.3470 65.6860
50.7858 45.8849 98.7982 72.1227 19.7810 57.6722 47.1088 14.9865 13.3171 62.7973
onsets_row1 = [1 5]
offsets_row1 = [3 8]
new_matrix =
NaN NaN NaN 33.5357 NaN NaN NaN NaN 43.2392 52.6876
31.8778 57.8525 39.5515 67.9728 33.4163 85.9442 71.2694 81.7547 82.5314 41.6799
42.4167 23.7284 36.7437 13.6553 69.8746 80.5489 50.0472 72.2440 8.3470 65.6860
50.7858 45.8849 98.7982 72.1227 19.7810 57.6722 47.1088 14.9865 13.3171 62.7973
How do I replace all the values between onsets and offsets?
thnx

3 Comments

Can you elaborate again, with small example?
onsets_row1 = [1 5]
offsets_row1 = [3 8]
what are those?
sure
I have a matrix of data and vectors of positions of start points and corresponding end points of noise for each row in the matrix.
Those points represent that start and end of noise that occured in the data. I need to delete (replace with NaN's) all the noise duration (cells between start point to relevant end point).
So if I have a 4X10 matrix,
and the start points (onsets) of noise vector of row 1 is:
onsets_row1 = [1 5]
End points (offsets) of noise vector of row 1 is:
offsets_row1 = [3 8]
It means I have 2 incedent of noise in the first row that occured in cells (1,1:3) and (1,5:8)
And need to replace the values in the noisey cells with NaN's.
Then go on to the next row that has a different vector of noise position.

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 16 Nov 2018
Edited: Stephen23 on 16 Nov 2018
For an aribtrary number of rows and an aribitrary number of indices using loops is about the most straightforward way of doing this, e.g.
M = [
29.668 48.861 67.914 33.536 89.092 61.767 97.868 81.815 43.239 52.688
31.878 57.853 39.552 67.973 33.416 85.944 71.269 81.755 82.531 41.680
42.417 23.728 36.744 13.655 69.875 80.550 50.047 72.244 8.347 65.686
50.786 45.885 98.798 72.123 19.781 57.672 47.109 14.987 13.317 62.797
];
X = {[1,5],[3,8];[2,6,8],[4,6,10]}; % {begR1,endR1;begR2,endR2;...;begRn,endRn}
for r = 1:size(X,1)
for c = 1:numel(X{r,1})
M(r,X{r,1}(c):X{r,2}(c)) = NaN;
end
end
disp(M)
Giving:
M =
NaN NaN NaN 33.536 NaN NaN NaN NaN 43.239 52.688
31.878 NaN NaN NaN 3.416 NaN 71.269 NaN NaN NaN
42.417 23.728 36.744 13.655 69.875 80.549 50.047 72.244 8.347 65.686
50.786 45.885 98.798 72.123 19.781 57.672 47.109 14.987 13.317 62.797

More Answers (2)

new_matrix = matrix;
for k=1:length(onsets_row1)
new_matrix(onsets_row1(k):offsets_row1(k)) = NaN;
end
Second solution, use this File Exchange MCOLON
new_matrix = matrix;
new_matrix(mcolon(onsets_row1,offsets_row1)) = NaN;

Categories

Find more on Interpolation of 2-D Selections in 3-D Grids in Help Center and File Exchange

Products

Release

R2018a

Asked:

on 16 Nov 2018

Edited:

on 16 Nov 2018

Community Treasure Hunt

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

Start Hunting!