how to get 2 outputs of my function?

2 views (last 30 days)
function [L,R]= LR(A)
R=A;
L=eye(size(A,1));
n=size(A,1);
for j=1:n-1
for i=(j+1):n
L(i,j)=R(i,j)/R(j,j);
R(i,:)=R(i,:)-L(i,j)*R(j,:);
end
end
end
Hi, i acually do not really understand, why I am only getting L, but not R,

Accepted Answer

Star Strider
Star Strider on 23 Mar 2019
The reason is likely the way you are calling your ‘LR’ funciton.
If you request both outputs, your function will deliver them:
A = ... ;
[L,R] = LR(A)
That call should return both outputs.
  3 Comments
tim123
tim123 on 23 Mar 2019
k I forgot to change a mistake in my code, that i changed. It works!
Star Strider
Star Strider on 23 Mar 2019
You didn’t state the matrix you are using as an argument, or what output you want from your funciton.
This works when I run it:
A = magic(4);
[L,R]= LR(A)
returning:
L =
1 0 0 0
0.3125 1 0 0
0.5625 0.56627 1 0
0.25 1.3012 -3 1
R =
16 2 3 13
0 10.375 9.0625 3.9375
0 -8.8818e-16 -0.81928 2.4578
0 -2.6645e-15 0 -2.6645e-15
Your function may only work for square matrices. If that is the case, you need to test for that, and return the appropriate outputs from your function, including an error condition and notification if the matrix argument is not square.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!