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:
Using only upper triangular part of matrix to divide another matrix

Subject: Using only upper triangular part of matrix to divide another matrix

From: Steve

Date: 21 Jun, 2011 14:43:06

Message: 1 of 5

Right now I am attempting to optimize my code as much as possible. I'm trying to optimize the code that does a./T. Is there any way to get the same results while only using the upper triangular portion of T or will that not help get speed gains? Also in order to deal with the NaN's on the resulting diagonal, after I divide I just set everything on the diagonal of the new matrix to 0.

An example of what a and T look like:
a =

     0 -1 -2 -3 -4
     1 0 -1 -2 -3
     2 1 0 -1 -2
     3 2 1 0 -1
     4 3 2 1 0

T =

     0 1 4 9 16
     1 0 1 4 9
     4 1 0 1 4
     9 4 1 0 1
    16 9 4 1 0

Thanks!

Subject: Using only upper triangular part of matrix to divide another matrix

From: Sean de

Date: 21 Jun, 2011 17:41:04

Message: 2 of 5

"Steve" wrote in message <itqalq$6og$1@newscl01ah.mathworks.com>...
> Right now I am attempting to optimize my code as much as possible. I'm trying to optimize the code that does a./T. Is there any way to get the same results while only using the upper triangular portion of T or will that not help get speed gains? Also in order to deal with the NaN's on the resulting diagonal, after I divide I just set everything on the diagonal of the new matrix to 0.
>
> An example of what a and T look like:
> a =
>
> 0 -1 -2 -3 -4
> 1 0 -1 -2 -3
> 2 1 0 -1 -2
> 3 2 1 0 -1
> 4 3 2 1 0
>
> T =
>
> 0 1 4 9 16
> 1 0 1 4 9
> 4 1 0 1 4
> 9 4 1 0 1
> 16 9 4 1 0
>
> Thanks!

Like this?

idx = true(size(a));
idx(tril(idx)) = false; %logical index of non-diagonal upper triangular portion

a(idx)./T(idx) %divide those parts

If you had a third matrix you could insert it
B(idx) = a(idx)./T(idx)

-Sean

Subject: Using only upper triangular part of matrix to divide another matrix

From: Greg Heath

Date: 21 Jun, 2011 21:55:43

Message: 3 of 5

On Jun 21, 10:43 am, "Steve " <vipquality...@gmail.com> wrote:
> Right now I am attempting to optimize my code as much as possible. I'm trying to optimize the code that does a./T. Is there any way to get the same results while only using the upper triangular portion of T or will that not help get speed gains? Also in order to deal with the NaN's on the resulting diagonal, after I divide I just set everything on the diagonal of the new matrix to 0.
>
> An example of what a and T look like:
> a =
>
>      0    -1    -2    -3    -4
>      1     0    -1    -2    -3
>      2     1     0    -1    -2
>      3     2     1     0    -1
>      4     3     2     1     0
>
> T =
>
>      0     1     4     9    16
>      1     0     1     4     9
>      4     1     0     1     4
>      9     4     1     0     1
>     16     9     4     1     0
>
> Thanks!

It is about 250% longer

close all, clear all, clc

warning off MATLAB:divideByZero

a = [ 0 -1 -2 -3 -4
     1 0 -1 -2 -3
     2 1 0 -1 -2
     3 2 1 0 -1
     4 3 2 1 0 ];


T = [0 1 4 9 16
     1 0 1 4 9
     4 1 0 1 4
     9 4 1 0 1
    16 9 4 1 0 ];

tic
for k = 1:1e5
    b = a./T;
    b(find(isnan(b))) = 0;
end
timeb = toc;

tic
for k = 1:1e5
    c = triu(a)./triu(T);
    c(find(isnan(c))) = 0;
    c = c-c';
end
timec = toc;

e = b-c
timeb = timeb
timec = timec
ratio = timec/timeb

Hope this helps.

Greg

Subject: Using only upper triangular part of matrix to divide another matrix

From: James Tursa

Date: 22 Jun, 2011 00:33:20

Message: 4 of 5

"Steve" wrote in message <itqalq$6og$1@newscl01ah.mathworks.com>...
> Right now I am attempting to optimize my code as much as possible. I'm trying to optimize the code that does a./T. Is there any way to get the same results while only using the upper triangular portion of T or will that not help get speed gains? Also in order to deal with the NaN's on the resulting diagonal, after I divide I just set everything on the diagonal of the new matrix to 0.
>
> An example of what a and T look like:
> a =
>
> 0 -1 -2 -3 -4
> 1 0 -1 -2 -3
> 2 1 0 -1 -2
> 3 2 1 0 -1
> 4 3 2 1 0
>
> T =
>
> 0 1 4 9 16
> 1 0 1 4 9
> 4 1 0 1 4
> 9 4 1 0 1
> 16 9 4 1 0
>
> Thanks!

Is your purpose purely a speed gain? How big is your matrix that you are actually working with?

James Tursa

Subject: Using only upper triangular part of matrix to divide another matrix

From: Steve

Date: 24 Jun, 2011 15:38:04

Message: 5 of 5

"James Tursa" wrote in message <itrd8g$k4q$1@newscl01ah.mathworks.com>...
> "Steve" wrote in message <itqalq$6og$1@newscl01ah.mathworks.com>...
> > Right now I am attempting to optimize my code as much as possible. I'm trying to optimize the code that does a./T. Is there any way to get the same results while only using the upper triangular portion of T or will that not help get speed gains? Also in order to deal with the NaN's on the resulting diagonal, after I divide I just set everything on the diagonal of the new matrix to 0.
> >
> > An example of what a and T look like:
> > a =
> >
> > 0 -1 -2 -3 -4
> > 1 0 -1 -2 -3
> > 2 1 0 -1 -2
> > 3 2 1 0 -1
> > 4 3 2 1 0
> >
> > T =
> >
> > 0 1 4 9 16
> > 1 0 1 4 9
> > 4 1 0 1 4
> > 9 4 1 0 1
> > 16 9 4 1 0
> >
> > Thanks!
>
> Is your purpose purely a speed gain? How big is your matrix that you are actually working with?
>
> James Tursa

Thanks so much for the help so far everyone. My purpose is purely a speed gain. I am going to be working with matrices that are on the scale of ~20000x20000. Will using loops to break down the matrix help for such large matrices?

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