function X = permuteConvOutput(X,permX,permXout,isPermXEmpty)
% Permuting the convolution layer output.

% Copyright 2022 The MathWorks, Inc.

% permX and permXout are permutation vectors that describe how to convert
% the input and output X respectively into the same dimension order (which
% happens to be 'S*SCB'), i.e., the conversion permutation from X_out to
% X_in, followed by permX must return the same permutation as permXout.
% Here, we want to return the data with the original permX. Combining two
% permutations is simply a reordering of the first with the second, which
% gives us the formula permOutToIn(permX) = permXout that satisfies the
% equality.
if isPermXEmpty
    permOutToIn = permXout;
else
    permOutToIn(permX) = permXout;
end
X = permute(X,permOutToIn);
end
