pinv operation in matlab
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
ANUSAYA SWAIN
el 20 de Abr. de 2024
Editada: Bruno Luong
el 20 de Abr. de 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].
0 comentarios
Respuesta aceptada
Más respuestas (1)
Bruno Luong
el 20 de Abr. de 2024
Editada: Bruno Luong
el 20 de Abr. de 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 comentarios
Bruno Luong
el 20 de Abr. de 2024
Editada: Bruno Luong
el 20 de Abr. de 2024
It's vectorized and potentially multithreaded via pagesvd, better I don't know. But it could be a stock function.
Ver también
Categorías
Más información sobre Linear Algebra en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!