How to perform pseudo inverse with set of elements in a matrix?
3 views (last 30 days)
Show older comments
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
on 24 Nov 2021
Please don't post the same question repeatedly. I closed your other question.
Answers (1)
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
Is that matrix a full rank matrix? No.
rank(A_43)
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
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]
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])
norm(C)
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)
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)))
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
0 Comments
See Also
Categories
Find more on Linear Least Squares in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!