Thread Subject: Addition Problem

Subject: Addition Problem

From: Joe Frank

Date: 13 Apr, 2009 13:48:00

Message: 1 of 5

I have this matrix

A =

1 2 3 1 2 3
4 5 6 4 5 6
7 8 9 7 8 9
4 5 6 4 5 6
1 2 3 1 2 3
4 5 6 4 5 6


I need to add
A(1,1) and A(1,2)
A(2,1) A(2,2), A(2,3)
A(3,2) A(3,3), A(3,4)
A(4,3) A(4,4), A(4,5)
A(5,4) A(5,5), A(5,6)
A(6,5) and A(6,6)

How do I add these? I know I can do A(1,1)+A(1,2) ...... But I dont wanna do it that way.. Are there any other better ways of doing it. Please help me out

Subject: Addition Problem

From: John D'Errico

Date: 13 Apr, 2009 14:19:00

Message: 2 of 5

"Joe Frank" <sascod@gmail.com> wrote in message <grvfqg$9bk$1@fred.mathworks.com>...
> I have this matrix
>
> A =
>
> 1 2 3 1 2 3
> 4 5 6 4 5 6
> 7 8 9 7 8 9
> 4 5 6 4 5 6
> 1 2 3 1 2 3
> 4 5 6 4 5 6
>
>
> I need to add
> A(1,1) and A(1,2)
> A(2,1) A(2,2), A(2,3)
> A(3,2) A(3,3), A(3,4)
> A(4,3) A(4,4), A(4,5)
> A(5,4) A(5,5), A(5,6)
> A(6,5) and A(6,6)
>
> How do I add these? I know I can do A(1,1)+A(1,2) ...... But I dont wanna do it that way.. Are there any other better ways of doing it. Please help me out

What does this do?

tril(A,1) - tril(A,-2)

Can you apply sum to the result to solve your
problem? What dimension will you sum over?

HTH,
John

Subject: Addition Problem

From: Matt

Date: 13 Apr, 2009 14:22:01

Message: 3 of 5

"Joe Frank" <sascod@gmail.com> wrote in message <grvfqg$9bk$1@fred.mathworks.com>...
> I have this matrix
>
> A =
>
> 1 2 3 1 2 3
> 4 5 6 4 5 6
> 7 8 9 7 8 9
> 4 5 6 4 5 6
> 1 2 3 1 2 3
> 4 5 6 4 5 6
>
>
> I need to add
> A(1,1) and A(1,2)
> A(2,1) A(2,2), A(2,3)
> A(3,2) A(3,3), A(3,4)
> A(4,3) A(4,4), A(4,5)
> A(5,4) A(5,5), A(5,6)
> A(6,5) and A(6,6)
>
> How do I add these? I know I can do A(1,1)+A(1,2) ...... But I dont wanna do it that way.. Are there any other better ways of doing it. Please help me out

One way:

M=size(A);
M=M-triu(M,2);
M=triu(M,-2);
result=sum(M.*A,2);

Subject: Addition Problem

From: Joe Frank

Date: 13 Apr, 2009 14:30:04

Message: 4 of 5

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <grvhkk$b7b$1@fred.mathworks.com>...
> "Joe Frank" <sascod@gmail.com> wrote in message <grvfqg$9bk$1@fred.mathworks.com>...
> > I have this matrix
> >
> > A =
> >
> > 1 2 3 1 2 3
> > 4 5 6 4 5 6
> > 7 8 9 7 8 9
> > 4 5 6 4 5 6
> > 1 2 3 1 2 3
> > 4 5 6 4 5 6
> >
> >
> > I need to add
> > A(1,1) and A(1,2)
> > A(2,1) A(2,2), A(2,3)
> > A(3,2) A(3,3), A(3,4)
> > A(4,3) A(4,4), A(4,5)
> > A(5,4) A(5,5), A(5,6)
> > A(6,5) and A(6,6)
> >
> > How do I add these? I know I can do A(1,1)+A(1,2) ...... But I dont wanna do it that way.. Are there any other better ways of doing it. Please help me out
>
> What does this do?
>
> tril(A,1) - tril(A,-2)
>
> Can you apply sum to the result to solve your
> problem? What dimension will you sum over?
>
> HTH,
> John

Thanks John.

Subject: Addition Problem

From: Matt

Date: 13 Apr, 2009 14:37:01

Message: 5 of 5

"Matt " <xys@whatever.com> wrote in message <grvhq9$mrs$1@fred.mathworks.com>...

> M=triu(M,-2);

Hmmm. Make that

M=triu(M,-1);

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com