Info

This question is closed. Reopen it to edit or answer.

How to replace/modify something in a call to function 1 from within function 2 (both in their separate files)

1 view (last 30 days)
The given task is to call a function from within another function, where both functions are handling matrices.
Now lets call this function 1 which is in its own file:
A = (1/dot(v,v))*(Ps'*Ps);
Function 1 is called with the command:
bpt = matok(P);
Now in another file in the same folder where function 1 is located (matok.m) we make another file containing function 2 that calls function 1:
bpt = matok(P);
What I wish B to do technically, is to return the result of the following (where D is a diagonal matrix):
OVERLOOK THIS LINE: B = (1/dot(v,v))*(Ps'*inv(D)*Ps*inv(D);
EDIT (corrected version of B here):
B = (1/dot(v,v))*(Ps*inv(D))'*Ps*inv(D);
But B should not "re-code" what has allready been written in function 1, the challenge/task is to call function 1 within function 2, and within function 2 we use the output of function 1 to end up with the result that B gives us. Also cause in the matrix world, A*B is not equal to B*A, then I can't simply multiply with inv(D) twice in the end. Now since Im not allowed to write B as is shown above, I was thinking of replacing (without altering function 1, doing the manipulation within function 2):
(Ps'*Ps)
with
(Ps'*inv(D)*Ps*inv(D)
which in some way I imagine should be possible, but since Im new to Matlab have no idea how to do or where even to start. Any ideas on how to achieve the desired result?
  2 Comments
dpb
dpb on 14 Mar 2015
The only commonality is the first term of 1/dot(v,v); if the object is to avoid recomputing that then only practical solution I see would be to return it as a second result from function A and pass that to function B which would then be
[bpt,dotvv]=maktok(P); % function A modified returns 1/dot() too
and
B = dotvv*(Ps'*inv(D)*Ps*inv(D);
where the argument list to function containing the above includes array dotvv
NB: you can't use 'B' as both the function name and the variable name by Matlab aliasing rules. Fortran would let you do that, Matlab won't.
If this misses the mark, repost. Showing the actual function code would undoubtedly help.
Olga Sirley
Olga Sirley on 14 Mar 2015
A small detail I missed (updated in main post):
The transpose shouldn't be of Ps in this
B = (1/dot(v,v))*(Ps'*inv(D))*Ps*inv(D);
But rather the transpose of both Ps and inv(D)
B = (1/dot(v,v))*(Ps*inv(D))'*Ps*inv(D);
I found this solution, but it might not be as compressed as it could've been and it seems a bit unelegant in my eyes, maybe there is an even shorter version?:
C = pinv(Ps') * A
E = (Ps*inv(D))' * C

Answers (0)

Community Treasure Hunt

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

Start Hunting!