pinv operation in matlab
7 views (last 30 days)
Show older comments
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].
0 Comments
Accepted Answer
More Answers (1)
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)
% Check correctness
for k = 1:size(A,3)
norm(pinv(A(:,:,k))-piA(:,:,k),'fro')
end
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
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.
See Also
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!