Hi,
A solution is to use MATLAB's discretize function, which automatically assigns each timing value to a bin defined by your chosen bin edges. This way, you can quickly group your spectra by these bin indices, store them in a cell array, and avoid complex loops or manual tolerance checks.
% Simulate data
x = linspace(0.1,20,101); % timing (1x101)
spectra = rand(100,1024) * 100; % spectra (100x1024)
spectra_wave = [linspace(300,1500,1024); spectra]; % (1025x100)
time_spectra = [x; spectra_wave']; % (1025x101)
% Binning using discretize
bin_edges = linspace(0.2,20,41); % 40 bins
time = time_spectra(1,:);
bin_idx = discretize(time, bin_edges);
binned_spectra = cell(1, length(bin_edges)-1);
for b = 1:length(binned_spectra)
idx = find(bin_idx == b);
if ~isempty(idx)
binned_spectra{b} = time_spectra(:, idx);
end
end
Refer to documentation below for more information:
Hope this helps!
