function A = mrdivide(A, B)
%/ Slash or right matrix divide for codistributed array
%   C = A / B
%   C = MRDIVIDE(A,B)
%   
%   A/B is the matrix division of B into A, and is computed using A/B = (B'\A')'.
%   See codistributed/MLDIVIDE for details on functionality and limitations.
%   
%   Example:
%   spmd
%       N = 1000;
%       D1 = codistributed.colon(1, N)';
%       D2 = D1 / 2;
%   end
%   
%   See also MRDIVIDE, CODISTRIBUTED, CODISTRIBUTED/COLON, CODISTRIBUTED/ZEROS.


%   Copyright 2006-2020 The MathWorks, Inc.

narginchk(2, 2);

distributedutil.CodistParser.verifyNonCodistributedInputs({A, B});

% Scalar case
if isscalar(B) || (isscalar(A) && ~ismatrix(B))
    A = codistributed.pElementwiseOp(@rdivide, A, B);
    return;
end

if iIsUnsupported(A) || iIsUnsupported(B)
    error(message('parallel:distributed:MrdivideNotSupported'));
end

transposeFirstMatrix = distributedutil.TransposeFlags.TRANSPOSE;
isMrdivide = true;
AT = transpose(A);

try
    [AT, rCond] = hMldivideAndMrdivide(transposeFirstMatrix, ...
        B, AT, isMrdivide);
catch ME
    throw(ME);
end

A = transpose(AT);

if ~isempty(rCond)
    printRcondWarning(rCond, superiorfloat(A));
end
end

%------------------------------------
function tf = iIsUnsupported(D)
tf = ~ismatrix(D) || ~(isfloat(D) || ischar(D) || islogical(D));
end
