How to perform pseudo inverse with set of elements in a matrix?

6 views (last 30 days)
Kalasagarreddi Kottakota
Kalasagarreddi Kottakota on 24 Nov 2021
Answered: John D'Errico on 24 Nov 2021
A=(1:12).';
B=(1:4).';
C = pinv(reshape(A,length(B),[]))*B;
D = pinv(A(1:4))*B % D should be equal to first element of C
Iam looking for a function that performs multiplication as follows:
Matrix B has 4 elements and matrix A has 12 elements. I want to perform pinv(A)*B, but not to all elements of A at a time. I need pinv(A(1:4))* B and pinv(A(5:6))*B and so on...
I used the formulation mentioned in the script, but getting wrong results? first element of D should be equal to D.
No loops, only vectorization
Could some one help me in this regard?
% answers of D and C
C =
0.8333
0.3333
-0.1667
D =
1.0000
  1 Comment
John D'Errico
John D'Errico on 24 Nov 2021
Please don't post the same question repeatedly. I closed your other question.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 24 Nov 2021
I think you misunderstand the linear algebra here.
A=(1:12).';
B=(1:4).';
A_43 = reshape(A,length(B),[]) % I'll create a variable for A_43, so we can use it several times
A_43 = 4×3
1 5 9 2 6 10 3 7 11 4 8 12
Is that matrix a full rank matrix? No.
rank(A_43)
ans = 2
If you now try to use a tool like pinv to effectively solve a system of equations, then you need to recognize the solution is not unique. So this now:
C = pinv(A_43)*B
C = 3×1
0.8333 0.3333 -0.1667
Yes, you think that it should be able to find the solution [1;0;0]. And it is true that
A_43*[1;0;0]
ans = 4×1
1 2 3 4
does return B. But since that matrix is not full rank, there are infintiely many possible solutions. The solution that pinv does find is one where the result has minimum norm over all possible solutions.
norm([1;0;0])
ans = 1
norm(C)
ans = 0.9129
Hoping to use a version of pinv that applies separately to every column of a matrix separately is NOT the solution. Understanding linear algebra is the solution. Sorry, but it is.
The total set of solutions to the general problem for this rank 2 matrix will be of the form:
syms t
allsols = pinv(A_43)*B + t*null(A_43)
allsols = 
And we can see that when t = 1/sqrt(6), this will yield the solution you think should be the correct one.
simplify(subs(allsols,t,1/sqrt(6)))
ans = 
But there are infintely many possible solutions. Note that \ does find the solution you think to be the correct one, but that is slightly fortuitous. And \ does complain about the singular matrix.
A_43\B
Warning: Rank deficient, rank = 2, tol = 1.875718e-14.
ans = 3×1
1.0000 0 0.0000

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!