MATLAB: Replace all values of cells between two positions

MATLABmatrix manipulation

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

Best Answer

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