pinv function is supported for dlarray input while the pageinv function is not supported

In the deep learning pipeline, I want to use inverse operation on every page of 3D dlarray object. I have tried pageinv function. I find that the pageinv function is not supported for dlarray object while the pinv function is supported for dlarray object.
what is the best practice to obtain the inverse of every page in a 3D dlarray object ?
dlA = dlarray(rand(3,3,10));
dlInv1 = pinv(dlA(:,:,1))
dlInv1 =
3×3 dlarray -32.8134 7.3618 38.7953 46.5918 -7.4349 -56.2980 21.2270 -6.1655 -22.3806
% pageinv(dlA)
I have used cellfun to inverse every page.
dlC = num2cell(dlA,[1 2]); % [1,1,B] cell array
dlInvB = cell2mat(cellfun(@pinv,dlC,"UniformOutput",false));
isequal(dlInvB(:,:,1),dlInv1)
ans = logical
1

1 Comment

In the deep learning pipeline, I want to use inverse operation on every page of 3D dlarray object.
If you're doing this because there is a step in your network which is the solution of a linear equation, then you are probably barking up the wrong tree. The network probably needs to be reformulated.

Sign in to comment.

 Accepted Answer

Answer from AI:
As of the latest MATLAB releases, pageinv does not directly support dlarray inputs. Standard matrix inversion functions like inv or pageinv cannot natively trace gradients for automatic differentiation inside custom deep learning loops or dlfeval. [1, 2]
To perform page-wise matrix inversion on a dlarray, you have two primary strategies depending on your network architecture.
Strategy 1: Use pagepinv (Best for R2025a or Later)
If you are running MATLAB R2025a or later, MathWorks introduced native dlarray support for pagepinv (page-wise Moore-Penrose pseudoinverse). For square, well-conditioned matrices, the pseudoinverse is identical to the standard inverse. [1]
  • Constraint: The dlarray must be unformatted.
% Create sample data (e.g., 3x3 matrices across a batch of 10)
X = rand(3, 3, 10);
dlX = dlarray(X); % Must be unformatted
% Compute page-wise pseudoinverse natively
dlY = pagepinv(dlX);
Strategy 2: Extract Data and Loop (For Gradients or Older Versions)
If you are on an older version of MATLAB or absolutely require standard pageinv, you must extract the underlying raw numeric data using extractdata, compute the page-wise inverse, and wrap it back into a dlarray. [1]
⚠️ Crucial Warning: This breaks the automatic differentiation graph. If this operation requires gradients during training, you must mathematically implement the backward pass derivative of a matrix inverse manually.
% 1. Extract raw numeric data from dlarray
raw_X = extractdata(dlX);
% 2. Compute page-wise inverse using standard MATLAB function
raw_Y = pageinv(raw_X);
% 3. Pack back into a dlarray
dlY = dlarray(raw_Y);

4 Comments

It would be advisable to use a regularized inverse, in case the iterations during learning stray near singularities,
At = pagetranspose(A);
M = pagemtimes(At,A) + lambda*I;
X = pagemtimes(pagepinv(M,lambda/2),At);
If A is large (or maybe even if not)
M = pagemtimes(A,'transpose',A,'none') + lambda*I; % (1)
X = pagemtimes(pagepinv(M,lambda/2),'none',A,'transpose');
IIRC correctly, for 2D array D the expression
Z = D'*D;
is guaranteed to return a symmetric result.
Does the guarantee of symmetry hold for the first term on the RHS of (1)?
Does pinv(Z) work differently given that issymmetric(Z) == true?
If so, does pagepinv also take advantage of symmetry? If every page is symmetric? If only some pages are symmetric?
Does pinv(Z) work differently given that issymmetric(Z) == true?
Probably not.
A=rand(100,100,1000);
S=A+pagetranspose(A); %symmetric
timeit(@() pagepinv(A))
ans = 0.2559
timeit(@() pagepinv(S))
ans = 0.2724
I wanted to look into the pagemtimes question, but I can't come up with an example where issymmetric(B*A) is false when B == A.'

Sign in to comment.

More Answers (1)

If your inputs to the operation will always be a 3x3XN stack, as in your example, then you can use the code below in conjunction with a functionLayer. This uses basic matrix arithemtic only, and so should not break the automatic differentiation graph. Keep in mind though that matrix inversion is not a continuous, differentiable operation (not everywhere), so you are taking your chances if you are using the standard derivative-based solvers of the Deep Learning Toolbox.
function B = inv3x3pages(A)
%INV3X3PAGES Page-wise inverse of a 3-by-3-by-N x B array.
%
% B = INV3X3PAGES(A) computes the inverse of each 3-by-3 page of A
% using the explicit adjugate/determinant formula.
Asiz= size(A);
A=A(:,:,:); %reshape to 3x3xN*B
a = A(1,1,:);
b = A(1,2,:);
c = A(1,3,:);
d = A(2,1,:);
e = A(2,2,:);
f = A(2,3,:);
g = A(3,1,:);
h = A(3,2,:);
i = A(3,3,:);
% Cofactors
C11 = e.*i - f.*h;
C12 = f.*g - d.*i;
C13 = d.*h - e.*g;
C21 = c.*h - b.*i;
C22 = a.*i - c.*g;
C23 = b.*g - a.*h;
C31 = b.*f - c.*e;
C32 = c.*d - a.*f;
C33 = a.*e - b.*d;
% Determinant
detA = a.*C11 + b.*C12 + c.*C13;
% inverse(A) = adj(A)/det(A)
B = zeros(size(A), 'like', A);
B(1,1,:) = C11 ./ detA;
B(1,2,:) = C21 ./ detA;
B(1,3,:) = C31 ./ detA;
B(2,1,:) = C12 ./ detA;
B(2,2,:) = C22 ./ detA;
B(2,3,:) = C32 ./ detA;
B(3,1,:) = C13 ./ detA;
B(3,2,:) = C23 ./ detA;
B(3,3,:) = C33 ./ detA;
B=reshape(B,Asiz);
end

Products

Release

R2025a

Asked:

on 28 Aug 2026 at 13:11

Edited:

about 5 hours ago

Community Treasure Hunt

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

Start Hunting!