I need to isolate certain values of a matrix based on whether

Illustration
Wynand - 2024-03-18T19:24:31+00:00
Question: I need to isolate certain values of a matrix based on whether

I need to isolate certain values of a matrix based on whether one of the values fall in a certain range I am writing a word recognition program that takes an array from 'audioread' and applies a few methods to it to get a frequency spectrum. This spectrum is saved into a cell aray with each cell containing the frequency peaks for a quater of the word at a time.   So for each word it returns a 4x1 cell array. Each of these cells need to be further subdivided to frequency bands An example of one such a cell follows     % ans = 2×2 % 24.9843526628176 32 % 5.44020124026728 62 The first column indicates amplitude and the second indicates the index. I need to split the matrix into divisions that are 30 wide each. So all 2nd column values from 0 to 30 must be together and all from 30 to 60 etc. All the way up to 300.   The following code exert is the function that I wrote for this application for context to the question. It is probably flawed and unoptimal so if there are glaring issues in it feel free to include commentary in any answers.     function FINAL_OUTPUT = FFT_Output(OG_Audio, Filter_count, Min_peak_distance) % Setup Sectional_Length = round(length(OG_Audio)/4); BM_Filtered_Signal = (blackman(length(OG_Audio))).*OG_Audio; First_Filter_Parameter = ((1/(Sectional_Length/100))*ones(1,(Sectional_Length/100))); Word_section = cell(4,1); FFT_Word = cell(4,1); FINAL_OUTPUT = cell(4,1); for i = 1:4 % Word sections Word_section{i} = BM_Filtered_Signal(((Sectional_Length*(i-1))+1):(Sectional_Length*i)); % Moving average filter j=0; while(j<=Filter_count) Word_section{i} = filter(First_Filter_Parameter, 1, Word_section{i}); j=j+1; end % FAST FOURIER TRANSFORM FFT_Word{i} = abs(fft(Word_section{i},length(Word_section{i}))); FFT_Word{i} = FFT_Word{i}(1:(round(length(FFT_Word{i})/(2)))); % Peak detection [M,I] = findpeaks(FFT_Word{i}, "MinPeakDistance",Min_peak_distance, "MinPeakHeight",(0.1*max(FFT_Word{i}))); % Identifying peak frequencies for comparisons FINAL_OUTPUT{i} = [M,I]; end end  

Expert Answer

Profile picture of Neeta Dsouza Neeta Dsouza answered . 2025-11-20

Here's a general approach - Discretize the data according to the groups and then split then into cells.

 

bins = 0:30:300;
idx = discretize(data, bins)
out = splitapply(@(x) {x}, data, idx)

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!