Regarding understanding of the following SVD code in matlab I earlier had a doubt regarding pseudo-inverse of a matrix, and earlier I was using pinv function. But one of the answers gave an alternative technique that is singular value decomposition method. I have a very little understanding of the method... can anyone please suggest some literature for understanding it properly so that I can understand the following code completely...or can explain the code in simple words..
please help..
R=[2,1,5;-2,-1,-5];
% pseudo inverse of A
[m,n]   = size(R);
% get SVD
[U,S,V] = svd(R);
% check rank
r       = rank(S);
SR      = S(1:r,1:r);
% make complete if rank is not full
SRc     = [SR^-1 zeros(r,m-r);zeros(n-r,r) zeros(n-r,m-r)];
% create inverse
A_inv   = V*SRc*U.';

g1 = zeros(size(k31));
g2 = zeros(size(k31));
g3 = zeros(size(k31));

 A: Wikipedia is your friend: Applications of the SVD. Look at the pseudo-inverse and rank sections.
A: The "pinv" function of matlab is using the singular value decomposition (SVD) as part of the "Moore-Penrose Pseudoinverse" as described here, so SVD is not neccessarily an alternative to "pinv".
The source code of the pinv function within Matlab is more simple than the code you posted, which may help with understanding it more easily.
Some more info that could help lead to an understanding:
Theoretical:
https://hadrienj.github.io/posts/Deep-Learning-Book-Series-2.9-The-Moore-Penrose-Pseudoinverse/
Practical:
https://www.dsprelated.com/showarticle/112.php
A: Every matrix
$$
\mathbf{A} \in \mathbb{C}^{m\times n}_{\rho}
$$
has a singular value decomposition
$$
  \mathbf{A} =
  \mathbf{U} \, \Sigma \, \mathbf{V}^{*} \\
$$
Putting this expansion into the least squares problem naturally produces the pseudoinverse solution as shown in How does the SVD solve the least squares problem?
The Moore-Penrose pseudoinverse is
$$
  \mathbf{A}^{\dagger} =
  \mathbf{V} \, \Sigma^{\dagger} \, \mathbf{U}^{*} \\
$$
This is the answer to the question "if given a matrix, how would I construct the Moore-Penrose pseudoinverse?"
But your question is different: "if given a matrix, and a data vector $b$ not in the null space, how would I find the Moore-Penrose solution?" As noted by @Shiyue, the pseudoinverse solution can be take less time than constructing the pseudoinverse:
What is the best way to compute the pseudoinverse of a matrix?
In summary, if you are trying to solve a system in the most efficient manner, rely on the intrinsic tools in MATLAB or Mathematica.
