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:
deletion of diagonal

Subject: deletion of diagonal

From: Aparna Sampath

Date: 9 Oct, 2010 02:15:07

Message: 1 of 5

Hi

I have a matrix of 100x 100 resultant from corrcoef () in MAtlab. I want to delete the diagonal elements of this matrix.

I tried doin this:

r1= diag(r);
r1=[];

but it is not working.



Any help! thanks in advance :)

Subject: deletion of diagonal

From: Roger Stafford

Date: 9 Oct, 2010 02:44:05

Message: 2 of 5

"Aparna Sampath" <aparna.sampath26@gmail.com> wrote in message <i8oj7b$plo$1@fred.mathworks.com>...
> Hi
>
> I have a matrix of 100x 100 resultant from corrcoef () in MAtlab. I want to delete the diagonal elements of this matrix.
>
> I tried doin this:
>
> r1= diag(r);
> r1=[];
>
> but it is not working. Any help! thanks in advance :)
- - - - - - - - - - -
  Do you truly want to delete the diagonal elements or merely want to replace them with zeros? The difference is significant! If you delete them, your matrix will have only 9900 elements and matlab will be forced to change it into a vector with that many elements. The following will do the deletion:

 r(1:101:10000) = [];

This will change the diagonal elements to zeros instead:

 r(1:101:10000) = 0;

Roger Stafford

Subject: deletion of diagonal

From: Nasser M. Abbasi

Date: 9 Oct, 2010 03:51:42

Message: 3 of 5

On 10/8/2010 7:44 PM, Roger Stafford wrote:


Roger,

Which do you recommend to zero diagonal elements?

EDU>> A=rand(3)
A =
     0.0987 0.6797 0.1068
     0.2619 0.1366 0.6538
     0.3354 0.7212 0.4942

EDU>> B=A; B(1:4:9)=0

B =

          0 0.6797 0.1068
     0.2619 0 0.6538
     0.3354 0.7212 0

EDU>> A(eye(size(A))~=0)=0

A =

          0 0.6797 0.1068
     0.2619 0 0.6538
     0.3354 0.7212 0

I never really looked more into which one is supposed to be 'better'. I
think the first is probably faster than the second method. Less
functions calls.

--Nasser

Subject: deletion of diagonal

From: Roger Stafford

Date: 9 Oct, 2010 04:41:03

Message: 4 of 5

"Nasser M. Abbasi" <nma@12000.org> wrote in message <i8oosd$g72$2@speranza.aioe.org>...
> Roger,
> Which do you recommend to zero diagonal elements?
>
> EDU>> A=rand(3)
> A =
> 0.0987 0.6797 0.1068
> 0.2619 0.1366 0.6538
> 0.3354 0.7212 0.4942
>
> EDU>> B=A; B(1:4:9)=0
>
> B =
> 0 0.6797 0.1068
> 0.2619 0 0.6538
> 0.3354 0.7212 0
>
> EDU>> A(eye(size(A))~=0)=0
>
> A =
> 0 0.6797 0.1068
> 0.2619 0 0.6538
> 0.3354 0.7212 0
>
> I never really looked more into which one is supposed to be 'better'. I
> think the first is probably faster than the second method. Less
> functions calls.
>
> --Nasser
- - - - - - - - -
  I would guess the first of those is faster, but I have learned that it takes some careful testing to be sure of assertions like that, and I haven't done the testing. The second method might be regarded as the more elegant one.

Roger Stafford

Subject: deletion of diagonal

From: Aparna Sampath

Date: 9 Oct, 2010 14:37:04

Message: 5 of 5

I think I will replace the diagonal values with zeros as they make my matrix more meaningful for the next step.
Thanks a lot! :)

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