Thread Subject: Contracting Matrices

Subject: Contracting Matrices

From: Philosophaie

Date: 27 Dec, 2008 16:25:03

Message: 1 of 8

I have a 4x4x4x4 matrices. I want to contract one of the indices and make a 4x4x4 matrix. The original matrix has both co and contra- varient components. Here is a excerpt from my code:

    for j=1:4
        for k=1:4
           for i=1:4
            Ricci(j,k)=Ricci(j,k)+Riemann(j,k,i,i)
        end
    end
end

Subject: Contracting Matrices

From: Roger Stafford

Date: 27 Dec, 2008 17:23:02

Message: 2 of 8

"Philosophaie" <ertlejack@sbcglobal.net> wrote in message <gj5ksv$1mq$1@fred.mathworks.com>...
> I have a 4x4x4x4 matrices. I want to contract one of the indices and make a 4x4x4 matrix. The original matrix has both co and contra- varient components. Here is a excerpt from my code:
>
> for j=1:4
> for k=1:4
> for i=1:4
> Ricci(j,k)=Ricci(j,k)+Riemann(j,k,i,i)
> end
> end
> end

  Try:

 temp = reshape(Riemann,4,4,[],1);
 Ricci = squeeze(sum(temp(:,:,1:4+1:4*4,1),3));

However it produces a 4x4 size, not 4x4x4.

Roger Stafford

Subject: Contracting Matrices

From: Philosophaie

Date: 27 Dec, 2008 18:49:01

Message: 3 of 8

> temp = reshape(Riemann,4,4,[],1);
> Ricci = squeeze(sum(temp(:,:,1:4+1:4*4,1),3));
> disp(Ricci(:,:))
this produces a 1x4 matrix

What am I doing wrong?

Subject: Contracting Matrices

From: Roger Stafford

Date: 27 Dec, 2008 21:06:01

Message: 4 of 8

"Philosophaie" <ertlejack@sbcglobal.net> wrote in message <gj5tat$jb2$1@fred.mathworks.com>...
> > temp = reshape(Riemann,4,4,[],1);
> > Ricci = squeeze(sum(temp(:,:,1:4+1:4*4,1),3));
> > disp(Ricci(:,:))
> this produces a 1x4 matrix
> What am I doing wrong?

  I would suggest doing the 'size' operator at each stage. You should get these results:

 size(Riemann) --> [4 4 4 4]
 size(temp) --> [4 4 16 1]
 size(temp(:,:,1:4+1:4*4,1)) --> [4 4 4 1]
 size(sum(temp(:,:,1:4+1:4*4,1),3)) --> [4 4 1 1]
 size(Ricci) --> [4 4]

Where does yours differ from this?

Roger Stafford

Subject: Contracting Matrices

From: Philosophaie

Date: 27 Dec, 2008 21:32:01

Message: 5 of 8

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gj65bp$dhc$1@fred.mathworks.com>...
> "Philosophaie" <ertlejack@sbcglobal.net> wrote in message <gj5tat$jb2$1@fred.mathworks.com>...
> > > temp = reshape(Riemann,4,4,[],1);
> > > Ricci = squeeze(sum(temp(:,:,1:4+1:4*4,1),3));
> > > disp(Ricci(:,:))
> > this produces a 1x4 matrix
> > What am I doing wrong?
>
> I would suggest doing the 'size' operator at each stage. You should get these results:
>
> size(Riemann) --> [4 4 4 4]
> size(temp) --> [4 4 16 1]
> size(temp(:,:,1:4+1:4*4,1)) --> [4 4 4 1]
> size(sum(temp(:,:,1:4+1:4*4,1),3)) --> [4 4 1 1]
> size(Ricci) --> [4 4]

My results were:
size(Riemann) --> [4 4 4 4]
size(temp) --> [4 4 16]
size(sum(temp(:,:,1:4+1:4*4,1),3)) --> [4 1]
size(Ricci) --> [4 1]

What does the "3" do in the sum command. Where am I going wrong?

Subject: Contracting Matrices

From: Roger Stafford

Date: 27 Dec, 2008 22:27:01

Message: 6 of 8

"Philosophaie" <ertlejack@sbcglobal.net> wrote in message <gj66sh$dfj$1@fred.mathworks.com>...
> ......
> My results were:
> size(Riemann) --> [4 4 4 4]
> size(temp) --> [4 4 16]
> size(sum(temp(:,:,1:4+1:4*4,1),3)) --> [4 1]
> size(Ricci) --> [4 1]
>
> What does the "3" do in the sum command. Where am I going wrong?

  If my understanding is correct, 'reshape' of a [4 4 4 4] array should have given you [4 4 16 1], not [4 4 16].

  What did you get for size(temp(:,:,1:4+1:4*4,1))? It should have been [4 4 4 1] or with the above result [4 4 4]. Given the above [4 4 16] result, the expression temp(:,:,1:4+1:4*4,1) should have given an error. Did it?

  In any case, the 'sum' action should have been along the third dimension and left you with a size of [4 4 1] or at worst [4 4]. It looks as though your 'sum' paid no attention to the '3' parameter, which directs summation to that third dimension. There should also have been an error indicated for the presence of the parameter '3' if your 'sum' wasn't recognizing a second parameter.

  I have no way of accounting for your results. What version of matlab did you use?

Roger Stafford

Subject: Contracting Matrices

From: Bruno Luong

Date: 28 Dec, 2008 09:20:03

Message: 7 of 8

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gj6a3l$nt3$1@fred.mathworks.com>...

>
> What did you get for size(temp(:,:,1:4+1:4*4,1))? It should have been [4 4 4 1] or with the above result [4 4 4]. Given the above [4 4 16] result, the expression temp(:,:,1:4+1:4*4,1) should have given an error. Did it?
>

Roger,

singleton in the last dimension is automatically removed by Matlab.

m x n x p x 1 array is exactly like m x n x p array.

Bruno

Subject: Contracting Matrices

From: Steven Lord

Date: 29 Dec, 2008 03:35:24

Message: 8 of 8


"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:gj65bp$dhc$1@fred.mathworks.com...
> "Philosophaie" <ertlejack@sbcglobal.net> wrote in message
> <gj5tat$jb2$1@fred.mathworks.com>...
>> > temp = reshape(Riemann,4,4,[],1);
>> > Ricci = squeeze(sum(temp(:,:,1:4+1:4*4,1),3));
>> > disp(Ricci(:,:))
>> this produces a 1x4 matrix
>> What am I doing wrong?
>
> I would suggest doing the 'size' operator at each stage. You should get
> these results:
>
> size(Riemann) --> [4 4 4 4]
> size(temp) --> [4 4 16 1]

Since SIZE drops trailing singleton dimensions, size(temp) should be [4 4
16]. Similarly, for the lines below, the trailing 1 is dropped.

*snip*

--
Steve Lord
slord@mathworks.com

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
matrices Philosophaie 27 Dec, 2008 11:30:05
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com