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:
add column matrix to each element of each diagonal in square matrix

Subject: add column matrix to each element of each diagonal in square matrix

From: Mech Princess

Date: 20 Nov, 2011 08:16:26

Message: 1 of 5

A(mxm) square (where m is odd from 3 to 99)
E(1xn) (where n is the number of diagonals of A)

i need to add each element of E to each element of the corresponding diagonal of A
Result is B(mxm)

1st diagonal B(1,1)=A(1,1)+E(1,1);
2nd diagonal B(2,1)=A(2,1)+E(1,2); B(1,2)=A(1,2)+E(1,2);
3rd diagonal B(3,1)=A(3,1)+E(1,3); B(2,2)=A(2,2)+E(1,3); B(1,3)=A(1,3)+E(1,3);
and so on ...
nth diagonal B(m,m)=A(m,m)+E(1,n);

I kind of got stick below. Appreciate help with finishing this or a new solution.
For example when m=5;
n=9;
     A=[1 2 3 4 5; 2 3 1 0 0; 1 1 2 3 5; 0 1 2 2 2; 3 3 1 1 1];
     E=[1 -1 0 3 2 -4 6 -4 1];
     [Dia, no] =spdiags(flipud(A));
     for i=1:n
        newDia(:,i)=Dia(:,i)+E(1,i);
     end
Answer B s hould be 5x5

Thank you very much

Subject: add column matrix to each element of each diagonal in square matrix

From: Matt J

Date: 20 Nov, 2011 17:30:27

Message: 2 of 5


Z=flipud(toeplitz(E(end:-1:1)));
 B=A+Z(1:m,1:m);

Subject: add column matrix to each element of each diagonal in square matrix

From: Roger Stafford

Date: 20 Nov, 2011 18:39:11

Message: 3 of 5

"Matt J" wrote in message <jabdfj$k16$1@newscl01ah.mathworks.com>...
> Z=flipud(toeplitz(E(end:-1:1)));
> B=A+Z(1:m,1:m);
- - - - - - - - - -
  Also you can use the 'hankel' function and avoid doing the flip:

 B = A + hankel(E(1:m),E(m:2*m-1));

Roger Stafford

Subject: add column matrix to each element of each diagonal in square matrix

From: Mech Princess

Date: 20 Nov, 2011 19:27:14

Message: 4 of 5

"Matt J" wrote in message <jabdfj$k16$1@newscl01ah.mathworks.com>...
>
> Z=flipud(toeplitz(E(end:-1:1)));
> B=A+Z(1:m,1:m);

Thank you

Subject: add column matrix to each element of each diagonal in square matrix

From: Mech Princess

Date: 20 Nov, 2011 19:27:14

Message: 5 of 5

"Roger Stafford" wrote in message <jabhge$40c$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <jabdfj$k16$1@newscl01ah.mathworks.com>...
> > Z=flipud(toeplitz(E(end:-1:1)));
> > B=A+Z(1:m,1:m);
> - - - - - - - - - -
> Also you can use the 'hankel' function and avoid doing the flip:
>
> B = A + hankel(E(1:m),E(m:2*m-1));
>
> Roger Stafford

Thank you

Tags for 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