Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
inverse matrix program not working, newbie

Subject: inverse matrix program not working, newbie

From: Arrad Dianarkop

Date: 16 Sep, 2010 19:43:06

Message: 1 of 3

Hi, I'm newbie here, so please bear with me.

I wonder if anyone can tell me what's wrong with this inverse matrix program
Here's the program:

function [Ainv]=INVMAT(A)
[n,n]=size(A);
I=zeros(n,n);
for i=1:n;
    I(i,i)=1;
end
m(1:n,1:n)=A;
m(1:n,n+1:2*n)=I;
for i=1:n;
    m(i,1:2*n)=m(i,1:2*n)/m(i,i);
    for k=1:n;
        if i~=k
            m(k,1:2*n)=m(k,1:2*n)-m(k,1)*m(i,1:2*n);
        end;
    end;
end
invrs=m(1:n,n+1:2*n);

A= [2 -1 0 0; -1 2 -1 0; 0 -1 2 -1; 0 0 -1 2]

When I run:

>>Ainv = INVMAT(A)

it gives:

Error in ==> INVMAT at 2
[n,n]=size(A);

??? Output argument "Ainv" (and maybe others) not assigned during call to
"D:\Program
Files\%location of the program in file%"

I did not create this program. I'm learning from it tho, also, can someone tell me what '~' in i~k means?

Thank you.

Rad

Subject: inverse matrix program not working, newbie

From: Sean

Date: 16 Sep, 2010 19:54:20

Message: 2 of 3

"Arrad Dianarkop" <adianarko@gmail.com> wrote in message <i6ts0a$54v$1@fred.mathworks.com>...
> Hi, I'm newbie here, so please bear with me.
>
> I wonder if anyone can tell me what's wrong with this inverse matrix program
> Here's the program:
>
> function [Ainv]=INVMAT(A)
> [n,n]=size(A);
> I=zeros(n,n);
> for i=1:n;
> I(i,i)=1;
> end
> m(1:n,1:n)=A;
> m(1:n,n+1:2*n)=I;
> for i=1:n;
> m(i,1:2*n)=m(i,1:2*n)/m(i,i);
> for k=1:n;
> if i~=k
> m(k,1:2*n)=m(k,1:2*n)-m(k,1)*m(i,1:2*n);
> end;
> end;
> end
> invrs=m(1:n,n+1:2*n);
>
> A= [2 -1 0 0; -1 2 -1 0; 0 -1 2 -1; 0 0 -1 2]
>
> When I run:
>
> >>Ainv = INVMAT(A)
>
> it gives:
>
> Error in ==> INVMAT at 2
> [n,n]=size(A);
>
> ??? Output argument "Ainv" (and maybe others) not assigned during call to
> "D:\Program
> Files\%location of the program in file%"
>
> I did not create this program. I'm learning from it tho, also, can someone tell me what '~' in i~k means?
>
> Thank you.
>
> Rad

Well where in this program do you see Ainv assigned? I don't see it anywhere. Maybe on the last line something like this would do it:
Ainv = invrs;
or just put invrs as the output argument instead of Ainv.

The ~ means logical not
e.g.
~[1 0 1]
ans = [0 1 0]
~[2.3 0 -17]
ans = [0 1 0] since everything is logical true except for 0.

Thus i~=k means if i doesn't equal k then enter if statement.

I would recommend reading the getting started documentation.

Subject: inverse matrix program not working, newbie

From: someone

Date: 16 Sep, 2010 19:59:22

Message: 3 of 3

"Arrad Dianarkop" <adianarko@gmail.com> wrote in message <i6ts0a$54v$1@fred.mathworks.com>...
> Hi, I'm newbie here, so please bear with me.
>
> I wonder if anyone can tell me what's wrong with this inverse matrix program
> Here's the program:
>
> function [Ainv]=INVMAT(A)
> [n,n]=size(A);
> I=zeros(n,n);
> for i=1:n;
> I(i,i)=1;
> end
> m(1:n,1:n)=A;
> m(1:n,n+1:2*n)=I;
> for i=1:n;
> m(i,1:2*n)=m(i,1:2*n)/m(i,i);
> for k=1:n;
> if i~=k
> m(k,1:2*n)=m(k,1:2*n)-m(k,1)*m(i,1:2*n);
> end;
> end;
> end
> invrs=m(1:n,n+1:2*n);
>
> A= [2 -1 0 0; -1 2 -1 0; 0 -1 2 -1; 0 0 -1 2]
>
> When I run:
>
> >>Ainv = INVMAT(A)
>
> it gives:
>
> Error in ==> INVMAT at 2
> [n,n]=size(A);
>
> ??? Output argument "Ainv" (and maybe others) not assigned during call to
> "D:\Program
> Files\%location of the program in file%"

% The function is supposed to return a variable called Ainv:
function [Ainv]=INVMAT(A)
% but you didn't assign a value to Ainv anywhere in the function.

% Try changing:
invrs=m(1:n,n+1:2*n);
% to
Ainv=m(1:n,n+1:2*n);

% OR change:
function [Ainv]=INVMAT(A)
% to
function [invrs]=INVMAT(A)

>
> I did not create this program. I'm learning from it tho, also, can someone tell me what '~' in i~k means?

% if i~=k means "if i is not equal to k then" ...


>
> Thank you.
>
> Rad

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us