pinv operation in matlab
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(1 个)
Bruno Luong
2024-4-20
编辑:Bruno Luong
2024-4-20
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 个评论
Bruno Luong
2024-4-20
编辑:Bruno Luong
2024-4-20
It's vectorized and potentially multithreaded via pagesvd, better I don't know. But it could be a stock function.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!