Clear Filters
Clear Filters

pinv operation in matlab

4 views (last 30 days)
ANUSAYA SWAIN
ANUSAYA SWAIN on 20 Apr 2024
Edited: Bruno Luong on 20 Apr 2024
I have a matrix X of size [32X32X10]. I want to do pinv(X). But it suggested to use pagesvd(X). Now the dimension becomes [32X1X10]. However I want the matrix dimension to remain same that is [32X32X10].

Accepted Answer

Voss
Voss on 20 Apr 2024
X = rand(32,32,10);
XI = pageinv(X);
size(XI)
ans = 1x3
32 32 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  3 Comments
Steven Lord
Steven Lord on 20 Apr 2024
From the pagepinv documentation page: "Introduced in R2024a"
Bruno Luong
Bruno Luong on 20 Apr 2024
Thanks somehow I missed pagepinv when I looked up earlier.

Sign in to comment.

More Answers (1)

Bruno Luong
Bruno Luong on 20 Apr 2024
Edited: Bruno Luong on 20 Apr 2024
NOTE: This code is useful fonly or users who run version greater than R2021a and before R2024a, where pagesvd is supported by pagepinv is not..
A = pagemtimes(rand(4,2,5), rand(2,3,5)); % This should be your 32 x 32 x 10 matrix
piA = pagepinv(A)
piA =
piA(:,:,1) = -0.7143 -0.1994 0.8830 0.5803 3.6830 0.8982 -3.7827 -2.1157 -3.5004 -0.9739 4.3082 2.8220 piA(:,:,2) = 1.7339 -1.6101 0.2768 0.1140 -0.5683 0.8934 0.0456 0.2565 -1.1307 1.3152 -0.0816 0.1389 piA(:,:,3) = -0.7173 0.8751 0.5408 -0.0777 3.3456 -2.3089 -1.7138 0.8681 -1.6643 1.5736 1.0465 -0.3106 piA(:,:,4) = 11.6667 8.3580 -12.7967 -5.3279 -3.8216 -2.6054 4.4286 1.9206 -10.5258 -7.3566 11.8744 5.0506 piA(:,:,5) = -0.1933 0.0253 -0.3618 0.9475 -0.6336 -0.0070 -0.9652 2.2255 1.4941 0.2434 1.7190 -3.0276
% Check correctness
for k = 1:size(A,3)
norm(pinv(A(:,:,k))-piA(:,:,k),'fro')
end
ans = 0
ans = 0
ans = 0
ans = 0
ans = 0
function piA = pagepinv(A, tol)
sizeA = size(A);
m = sizeA(1);
n = sizeA(2);
if min(m,n) == 0
piA = zeros([n,m,sizeA(3:end)], 'like', A));
return
end
A = reshape(A,m,n,[]);
p = size(A,3);
[U,s,V]=pagesvd(A,'econ','vector');
if nargin >= 2
% user providestol, scalae or vector of length p
tol = reshape(tol, 1, 1, []);
else
smax = s(1,:,:); % ,porms of matrices
tol = max(m,n)*eps(smax);
end
s(s <= tol) = Inf;
S = reshape(s,1,size(V,2),p);
piA = pagemtimes(V./S,'none',U,'ctranspose');
piA = reshape(piA,[n,m,sizeA(3:end)]);
end
  2 Comments
Paul
Paul on 20 Apr 2024
Is this better than looping over the pages and calling pinv on each?
Bruno Luong
Bruno Luong on 20 Apr 2024
Edited: Bruno Luong on 20 Apr 2024
It's vectorized and potentially multithreaded via pagesvd, better I don't know. But it could be a stock function.

Sign in to comment.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!