pinv operation in matlab
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Plus de réponses (1)
Bruno Luong
le 20 Avr 2024
Modifié(e) : Bruno Luong
le 20 Avr 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 commentaires
Bruno Luong
le 20 Avr 2024
Modifié(e) : Bruno Luong
le 20 Avr 2024
It's vectorized and potentially multithreaded via pagesvd, better I don't know. But it could be a stock function.
Voir également
Catégories
En savoir plus sur Linear Algebra dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!