Thread Subject: inv(rand(6,6,2)) question

Subject: inv(rand(6,6,2)) question

From: Hydroman S

Date: 24 Nov, 2008 20:22:02

Message: 1 of 10

The question is some what related to my earlier post about matix division:

How come x=inv(rand(3,3,2));
gives this error:

>> x=inv(rand(6,6,2))
??? Error using ==> inv
Input arguments must be 2-D.

shouldn't each of the 2 matrcies in rand(6,6,2) give us a separate inverse?

Subject: inv(rand(6,6,2)) question

From: Bruno Luong

Date: 24 Nov, 2008 20:28:02

Message: 2 of 10

"Hydroman S" <amirgsalem@gmail.com> wrote in message <ggf2da$898$1@fred.mathworks.com>...

>
> shouldn't each of the 2 matrcies in rand(6,6,2) give us a separate inverse?
>

No.

Bruno

Subject: inv(rand(6,6,2)) question

From: Hydroman S

Date: 24 Nov, 2008 20:37:01

Message: 3 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ggf2oi$dfo$1@fred.mathworks.com>...
> "Hydroman S" <amirgsalem@gmail.com> wrote in message <ggf2da$898$1@fred.mathworks.com>...
>
> >
> > shouldn't each of the 2 matrcies in rand(6,6,2) give us a separate inverse?
> >
>
> No.
>
> Bruno

fine, now if I put it in a loop, I would think that it would work, but it also doesn't:

for i=1:6
x=inv(rand(3,3,i));
end

??? Error using ==> inv
Input arguments must be 2-D.



I also applied squeeze, but it does not work as well. My problem is why? since inside the loop, rand(3,3,1) is gives a square matrix

for i=1:2
x=squeeze(inv(rand(3,3,i)));
end

Subject: inv(rand(6,6,2)) question

From: alistair templeton

Date: 24 Nov, 2008 21:07:02

Message: 4 of 10


>
> I also applied squeeze, but it does not work as well. My problem is why? since inside the loop, rand(3,3,1) is gives a square matrix
>
> for i=1:2
> x=squeeze(inv(rand(3,3,i)));
> end
>
>

what about when i=2?

Subject: inv(rand(6,6,2)) question

From: Bruno Luong

Date: 24 Nov, 2008 21:16:03

Message: 5 of 10

"Hydroman S" <amirgsalem@gmail.com> wrote in message <ggf39d$mlm$1@fred.mathworks.com>...

>
> fine, now if I put it in a loop, I would think that it would work, but it also doesn't:
>
> for i=1:6
> x=inv(rand(3,3,i));
> end

Your loop cannot work

let's decompose, when i=2

rand(3,3,i) returns rand(3,3,2).

Similar issue occurs for i=3,4,5,6.

It's still a three dimensional array, and MATLAB INV does like it.

What you need is create the 3D array BEFORE the for-loop, then loop over the last indice. You must put a squeeze somewhere to reduce the dimension.

I let you figure out how to do that.

Bruno

Subject: inv(rand(6,6,2)) question

From: Hydroman S

Date: 24 Nov, 2008 21:23:02

Message: 6 of 10

"Alistair Templeton" <bigalt2000@yahoo.com> wrote in message <ggf51m$pdq$1@fred.mathworks.com>...
>
> >
> > I also applied squeeze, but it does not work as well. My problem is why? since inside the loop, rand(3,3,1) is gives a square matrix
> >
> > for i=1:2
> > x=squeeze(inv(rand(3,3,i)));
> > end
> >
> >
>
> what about when i=2?

Point well taken. sorry I wasn't thinking clearly. So what would be the proper way to find the inv in this case?

Subject: inv(rand(6,6,2)) question

From: Hydroman S

Date: 24 Nov, 2008 21:25:03

Message: 7 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ggf5ij$61d$1@fred.mathworks.com>...
> "Hydroman S" <amirgsalem@gmail.com> wrote in message <ggf39d$mlm$1@fred.mathworks.com>...
>
> >
> > fine, now if I put it in a loop, I would think that it would work, but it also doesn't:
> >
> > for i=1:6
> > x=inv(rand(3,3,i));
> > end
>
> Your loop cannot work
>
> let's decompose, when i=2
>
> rand(3,3,i) returns rand(3,3,2).
>
> Similar issue occurs for i=3,4,5,6.
>
> It's still a three dimensional array, and MATLAB INV does like it.
>
> What you need is create the 3D array BEFORE the for-loop, then loop over the last indice. You must put a squeeze somewhere to reduce the dimension.
>
> I let you figure out how to do that.
>
> Bruno

Thanks Bruno, let me give it shot...

Subject: inv(rand(6,6,2)) question

From: chaintzean

Date: 24 Nov, 2008 21:29:01

Message: 8 of 10

"Hydroman S" <amirgsalem@gmail.com> wrote in message <ggf39d$mlm$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ggf2oi$dfo$1@fred.mathworks.com>...
> > "Hydroman S" <amirgsalem@gmail.com> wrote in message <ggf2da$898$1@fred.mathworks.com>...
> >
> > >
> > > shouldn't each of the 2 matrcies in rand(6,6,2) give us a separate inverse?
> > >
> >
> > No.
> >
> > Bruno
>
> fine, now if I put it in a loop, I would think that it would work, but it also doesn't:
>
> for i=1:6
> x=inv(rand(3,3,i));
> end
>
> ??? Error using ==> inv
> Input arguments must be 2-D.
>
>
>
> I also applied squeeze, but it does not work as well. My problem is why? since inside the loop, rand(3,3,1) is gives a square matrix
>
> for i=1:2
> x=squeeze(inv(rand(3,3,i)));
> end
>
>

It seems like you don't understand how rand() works. If what you really need to do is to inverse sub-matrices of a random 3-dimensional matrix (which I doubt), then looping on it would have to be done that way:

X = rand(6,6,2);

for i=1:2
    X_inv(:,:,i) = inv(squeeze(X(:,:,i)));
end

rand(3,3,i) generates a 3x3x(i) array which cannot be inverted (except for i=1, of course). Also, why would you squeeze outside of the inv call? By the way, you don't even need to squeeze in my example above, I just wanted to illustrate proper use.

Subject: inv(rand(6,6,2)) question

From: Doug Schwarz

Date: 24 Nov, 2008 21:36:57

Message: 9 of 10

In article <ggf39d$mlm$1@fred.mathworks.com>,
 "Hydroman S" <amirgsalem@gmail.com> wrote:

> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> <ggf2oi$dfo$1@fred.mathworks.com>...
> > "Hydroman S" <amirgsalem@gmail.com> wrote in message
> > <ggf2da$898$1@fred.mathworks.com>...
> >
> > >
> > > shouldn't each of the 2 matrcies in rand(6,6,2) give us a separate
> > > inverse?
> > >
> >
> > No.
> >
> > Bruno
>
> fine, now if I put it in a loop, I would think that it would work, but it
> also doesn't:
>
> for i=1:6
> x=inv(rand(3,3,i));
> end
>
> ??? Error using ==> inv
> Input arguments must be 2-D.

Think about what you are doing. The first time through the loop you
generate a 3x3x1 matrix and that works. The second time through the
loop you generate a 3x3x2 matrix and that doesn't work.

What you want to do is generate the matrix once and then pick each plane
in turn:

  A = rand(6,6,n);
  for i = 1:n
      B(:,:,i) = inv(A(:,:,i));
  end

(Of course, you probably shouldn't be using inverses in the first place.)

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: inv(rand(6,6,2)) question

From: Hydroman S

Date: 24 Nov, 2008 21:42:02

Message: 10 of 10

"chaintzean " <chaintzean@hotmail.com> wrote in message <ggf6at$ino$1@fred.mathworks.com>...
> "Hydroman S" <amirgsalem@gmail.com> wrote in message <ggf39d$mlm$1@fred.mathworks.com>...
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ggf2oi$dfo$1@fred.mathworks.com>...
> > > "Hydroman S" <amirgsalem@gmail.com> wrote in message <ggf2da$898$1@fred.mathworks.com>...
> > >
> > > >
> > > > shouldn't each of the 2 matrcies in rand(6,6,2) give us a separate inverse?
> > > >
> > >
> > > No.
> > >
> > > Bruno
> >
> > fine, now if I put it in a loop, I would think that it would work, but it also doesn't:
> >
> > for i=1:6
> > x=inv(rand(3,3,i));
> > end
> >
> > ??? Error using ==> inv
> > Input arguments must be 2-D.
> >
> >
> >
> > I also applied squeeze, but it does not work as well. My problem is why? since inside the loop, rand(3,3,1) is gives a square matrix
> >
> > for i=1:2
> > x=squeeze(inv(rand(3,3,i)));
> > end
> >
> >
>
> It seems like you don't understand how rand() works. If what you really need to do is to inverse sub-matrices of a random 3-dimensional matrix (which I doubt), then looping on it would have to be done that way:
>
> X = rand(6,6,2);
>
> for i=1:2
> X_inv(:,:,i) = inv(squeeze(X(:,:,i)));
> end
>
> rand(3,3,i) generates a 3x3x(i) array which cannot be inverted (except for i=1, of course). Also, why would you squeeze outside of the inv call? By the way, you don't even need to squeeze in my example above, I just wanted to illustrate proper use.
>

Thanks chaintzean this also answes my question.

Tags for this Thread

Everyone's Tags:

inv

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
inv Hydroman S 24 Nov, 2008 15:25:06
rssFeed for this Thread

Contact us at files@mathworks.com