Error using 'splitapply' when the first bin has no entries

I am writing a function to take the values from a cell array, split them according the the values in the second column of each cell and then get the average for each cell.
I am running into an issue where in one of the instances the first bin has no values that go in it and then I get this issue
Error using splitapply (line 111)
For N groups, every integer between 1 and N must occur at least once in the vector of group numbers.
I have looked around at old question but am unable to find a solution to my problem.
Note 1 - A function that returns an (:,2) array. The first column contained amplitudes to frequency spikes
Note 2 - The location the error occurs at
Thank you
%function z = Band_Divider(Frequencies, band_width)
% --- TEST SETUP ---
clear;
x = audioread("Recordings\Speaker Male\2\21.wav");
Frequencies = FFT_Output(x, 2, 5); %% NOTE 1 This function takes an audio signal and identifies the peak frequencies per word section. The frequencies are stored in a cell
band_width = 30;
% --- END ---
% --- SETUP ---
Word_Breakdown = zeros(4,(300/band_width));
bins = 0:(band_width):300;
% --- LOOP ---
for i=1:4
array = Frequencies{i}
Seperators = discretize(array(:,2), bins)
Bands = splitapply(@(x) {x}, array, Seperators) % NOTE 2 The error occurs at this line
for j=1:height(Bands)
Band_Value = Bands{j}(:,1)
Word_Breakdown(i,j) = mean(Band_Value);
end
end
%end
%--- TEST PRINTS ---
Word_Breakdown
%--- END ---

 Accepted Answer

That is a limitation (for the lack of a better word) of splitapply.
Please try this method with accumarray -
array = Frequencies{i};
Seperators = discretize(array(:,2), bins);
Bands = accumarray(Seperators, array, [], @(x) {x})

5 Comments

Thank you for the answer. This returns some errors of it's own but I will try to fix them myself first
You are welcome!
You can ask any follow up questions you have here, I'll be happy to assist you with them.
Thank you. Sorry for bothering you so much with this question but I can't seem to resolve the new error either.
This is the code
% function z = Band_Divider(Frequencies, band_width)
% --- TEST SETUP ---
clear;
x = audioread("Recordings\Speaker Male\1\11.wav");
Frequencies = FFT_Output(x, 2, 5);
band_width = 30;
% --- END ---
% --- SETUP ---
Word_Breakdown = zeros(4,(300/band_width));
bins = 0:(band_width):300;
% --- LOOP ---
array = Frequencies{1}
Seperators = discretize(array(:,2), bins)
Bands = accumarray(Seperators, array, [], @(x) {x})
% end
%--- TEST PRINTS ---
Word_Breakdown;
%--- END ---
This is my variable values and the final part is the error code I get
array = 10×2
0.0438 6.0000
0.0560 14.0000
0.1153 20.0000
0.0303 27.0000
0.0777 33.0000
0.1642 39.0000
0.0381 47.0000
0.0239 54.0000
0.0223 62.0000
0.0139 72.0000
Seperators = 10×1
1
1
1
1
2
2
2
2
3
3
Error using accumarray
Second input VAL must be a vector with one element for each row in SUBS, or a scalar.
"Second input VAL must be a vector with one element for each row in SUBS, or a scalar."
Is your 2nd input a vector or a scalar? (hint: no)
I am guessing that you intended to only input the first column of ARRAY:
accumarray(Seperators, array(:,1), [], @(x) {x})
Is there a good reason why you don't simply calculate the mean directly?:
accumarray(Seperators, array(:,1), [], @mean)
I was inteding to use only the first column yes, that was a oversight on my part.
There isn't no. Mostly a lack of understanding the function and documentation.
Thank you for the help. Implementing your new hints did solve this issue. On to the next

Sign in to comment.

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 19 Oct 2023

Commented:

on 20 Oct 2023

Community Treasure Hunt

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

Start Hunting!