Thread Subject: bsxfun help - transform diagonal of an array of matrices

Subject: bsxfun help - transform diagonal of an array of matrices

From: Rajgopal

Date: 16 Aug, 2009 19:19:03

Message: 1 of 6

Hi everyone,

I have an array of 4*4 square diagonal matrices. I want to transform the diagonals of all the matrices to the corresponding sum of the off-diagonals. Any shortcuts using bsxfun...I thought of one, but it involved a couple of bsxfuns and a reshape and I'm sure there is a better way to do it.

X = repmat(single(5),[4 4 no_of_matrices]);

Thanks a lot,
Raj

Subject: bsxfun help - transform diagonal of an array of matrices

From: arun

Date: 17 Aug, 2009 02:44:06

Message: 2 of 6

On Aug 16, 9:19 pm, "Rajgopal " <run...@gmail.com> wrote:
> Hi everyone,
>
> I have an array of 4*4 square diagonal matrices. I want to transform the diagonals of all the matrices to the corresponding sum of the off-diagonals. Any shortcuts using bsxfun...I thought of one, but it involved a couple of bsxfuns and a reshape and I'm sure there is a better way to do it.
>
> X = repmat(single(5),[4 4 no_of_matrices]);
>
> Thanks a lot,
> Raj

I don't quite follow your question. Could you illustrate your problem
with an example?

Subject: bsxfun help - transform diagonal of an array of matrices

From: Matt

Date: 17 Aug, 2009 07:56:02

Message: 3 of 6

"Rajgopal " <runraj@gmail.com> wrote in message <h69m37$jek$1@fred.mathworks.com>...
> Hi everyone,
>
> I have an array of 4*4 square diagonal matrices. I want to transform the diagonals of all the matrices to the corresponding sum of the off-diagonals.
-------------


Wouldn't that make the result zero? If your 4x4 matrices are diagonal to begin with, the sum of the off-diagonals is zero.

Subject: bsxfun help - transform diagonal of an array of matrices

From: Rajgopal

Date: 17 Aug, 2009 08:37:02

Message: 4 of 6

Apologies for the error.

These are 4*4 square matrices.

An example would be something like

 X(:,:,1) = 1 2 0 0
                 0 6 0 3
                 4 4 0 5
                 3 0 9 0

would become

                2 2 0 0
                0 3 0 3
                4 4 13 5
                3 0 9 12

The same operation would be applied on the other matrices in the X array.

Thanks for the responses.


                 


"Matt " <xys@whatever.com> wrote in message <h6b2ei$dpi$1@fred.mathworks.com>...
> "Rajgopal " <runraj@gmail.com> wrote in message <h69m37$jek$1@fred.mathworks.com>...
> > Hi everyone,
> >
> > I have an array of 4*4 square diagonal matrices. I want to transform the diagonals of all the matrices to the corresponding sum of the off-diagonals.
> -------------
>
>
> Wouldn't that make the result zero? If your 4x4 matrices are diagonal to begin with, the sum of the off-diagonals is zero.

Subject: bsxfun help - transform diagonal of an array of matrices

From: Loren Shure

Date: 17 Aug, 2009 10:33:53

Message: 5 of 6

In article <h6b4re$gvl$1@fred.mathworks.com>, runraj@gmail.com says...
> Apologies for the error.
>
> These are 4*4 square matrices.
>
> An example would be something like
>
> X(:,:,1) = 1 2 0 0
> 0 6 0 3
> 4 4 0 5
> 3 0 9 0
>
> would become
>
> 2 2 0 0
> 0 3 0 3
> 4 4 13 5
> 3 0 9 12
>
> The same operation would be applied on the other matrices in the X array.
>
> Thanks for the responses.
>
>
>
>
>
> "Matt " <xys@whatever.com> wrote in message <h6b2ei$dpi$1@fred.mathworks.com>...
> > "Rajgopal " <runraj@gmail.com> wrote in message <h69m37$jek$1@fred.mathworks.com>...
> > > Hi everyone,
> > >
> > > I have an array of 4*4 square diagonal matrices. I want to transform the diagonals of all the matrices to the corresponding sum of the off-diagonals.
> > -------------
> >
> >
> > Wouldn't that make the result zero? If your 4x4 matrices are diagonal to begin with, the sum of the off-diagonals is zero.
>

Not sure you need bsxfun, but maybe you do. First take row sums,
removing the diagonal elements. Then replace existing diags with these
new values.

newdiags = sum(x,2)-diag(x);
x(1:(size(x,2)+1):end) = newdiags;


--
Loren
http://blogs.mathworks.com/loren

Subject: bsxfun help - transform diagonal of an array of matrices

From: Rajgopal

Date: 17 Aug, 2009 22:51:01

Message: 6 of 6

Thanks Loren...I will need bsxfun though since diag doesn't really work on >2D matrices.

Loren Shure <loren@mathworks.com> wrote in message <MPG.24f2fc1e565b6000989a11@news.mathworks.com>...
> In article <h6b4re$gvl$1@fred.mathworks.com>, runraj@gmail.com says...
> > Apologies for the error.
> >
> > These are 4*4 square matrices.
> >
> > An example would be something like
> >
> > X(:,:,1) = 1 2 0 0
> > 0 6 0 3
> > 4 4 0 5
> > 3 0 9 0
> >
> > would become
> >
> > 2 2 0 0
> > 0 3 0 3
> > 4 4 13 5
> > 3 0 9 12
> >
> > The same operation would be applied on the other matrices in the X array.
> >
> > Thanks for the responses.
> >
> >
> >
> >
> >
> > "Matt " <xys@whatever.com> wrote in message <h6b2ei$dpi$1@fred.mathworks.com>...
> > > "Rajgopal " <runraj@gmail.com> wrote in message <h69m37$jek$1@fred.mathworks.com>...
> > > > Hi everyone,
> > > >
> > > > I have an array of 4*4 square diagonal matrices. I want to transform the diagonals of all the matrices to the corresponding sum of the off-diagonals.
> > > -------------
> > >
> > >
> > > Wouldn't that make the result zero? If your 4x4 matrices are diagonal to begin with, the sum of the off-diagonals is zero.
> >
>
> Not sure you need bsxfun, but maybe you do. First take row sums,
> removing the diagonal elements. Then replace existing diags with these
> new values.
>
> newdiags = sum(x,2)-diag(x);
> x(1:(size(x,2)+1):end) = newdiags;
>
>
> --
> Loren
> http://blogs.mathworks.com/loren

Tags for this Thread

Everyone's Tags:

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
bsxfun Rajgopal 16 Aug, 2009 15:24:03
diagonal Rajgopal 16 Aug, 2009 15:24:03
rssFeed for this Thread

Contact us at files@mathworks.com