Parfor: Unable to perform assignment because the size of the left side is 98-by-50 and the size of the right side is 107-by-50

Hello,
I have been trying to modify the speech command recognition example (https://uk.mathworks.com/help/audio/examples/Speech-Command-Recognition-Using-Deep-Learning.html) by adding in one of my own words into the data set.
However when I get to the feature extraction for the valdation set, I get the error "Unable to perform assignment because the size of the left side is 98-by-50 and the size of the right side is 107-by-50" on the line:
parfor ii = 1:numPar
I am unsure of why this section is flagging an error when my test set did this without any hiccups. Any help on this would be much appreciated, thank you!
Here is the section of code it occurs in:
%%
% Perform the feature extraction steps described above to the validation
% set.
if ~isempty(ver('parallel'))
pool = gcp;
numPar = numpartitions(adsValidation,pool);
else
numPar = 1;
end
parfor ii = 1:numPar
subds = partition(adsValidation,numPar,ii);
XValidation = zeros(numHops,numBands,1,numel(subds.Files));
for idx = 1:numel(subds.Files)
x = read(subds);
xPadded = [zeros(floor((segmentSamples-size(x,1))/2),1);x;zeros(ceil((segmentSamples-size(x,1))/2),1)];
XValidation(:,:,:,idx) = extract(afe,xPadded);
end
XValidationC{ii} = XValidation;
end
XValidation = cat(4,XValidationC{:});
XValidation = XValidation/unNorm;
XValidation = log10(XValidation + epsil);

 Accepted Answer

Unfortunately, error reporting from parfor has some limitations, and specifically it cannot indicate the precise line within the loop body where the error is occurring.
Having said that, I cannot see in your loop body any places where you're performing a 2-dimensional assignment operation to generate the error that you're observing... (although my guess would be the assignment into XValidation).
One option in this case is to place the entire body of your parfor loop into a separate function - that way when things do fail, you should get a more precise error location. Something like this:
parfor ii = 1:numPar
% Unfortunately, it looks like you'll need to pass through quite a number of parameters.
XValidationC{ii} = loopBodyFcn(adsValidation, numPar, ii, numHops, numBands); % more here
end

5 Comments

The third dimension of XValidation is 1, so
XValidation(:,:,:,idx) = extract(afe,xPadded);
is a numHops x numBands x 1 left hand side, which would "collapse" to numHops x numBands. That is where the 2D assignment is.
Thanks for this Edric and Walter, I have wittled it down thanks to you two! :)
Walter was right that it was
XValidation(:,:,:,idx) = extract(afe,xPadded);
So I changed the size of XValidation so that numHops was 107 to match the right hand side.
However, after doing this, it now gives the error "Unable to perform assignment because the size of the left side is 107-by-50 and the size of the right side is 98-by-50" so it has swapped! If anyone has any idea how else to change it I would be very grateful.
Thanks again
One thing to check - what happens if you replace parfor with for? It seems to me like you should get an error doing that (which might make life easier to debug).
I think the underlying problem is that the result of extract(afe,xPadded) can change size - probably because x changes size. If you can't reproduce the problem using a for loop, I'd add some fprintf statements into your code with some info to help you work out exactly where the problem occurred, like so:
...
newX = extract(afe, xPadded);
if ~isequal(size(newX), size(XValidation(:,:,:,idx))
fprintf('Size mismatch on iteration: %d\n', ii);
fprintf('Size of x: %s', num2str(size(x)));
end
Thanks Edric, using a for loop gave a much better understanding of the error. Turned out to be tha the new data I was adding was sampled at a much higher rate so the files were bigger. This in turn meant that it wasn't the same size as the other samples which was why this error occured! Thanks for the help :)

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2020a

Tags

Community Treasure Hunt

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

Start Hunting!