Thread Subject: Avoiding Nested For loops

Subject: Avoiding Nested For loops

From: Gary

Date: 22 Dec, 2011 02:13:08

Message: 1 of 4

Hi,

I have been working on trying to improve my CPU time. I generated a set of data and have arranged them in such a way where I have three large matrices that are NxNx300 sample points. I implemented this by using cell arrays. When I run my code, I notice that it takes 6 hours at least to execute one section of my code and that is due to nested for loops that I have. I would like to find a way to fetch data one at a time for
3 huge cell array matrices (T1, T2 and T3) that are NxNx300 and perform the following multiplication T_tot=T1*T2*T3 so that my result is T_tot is NxNx300. Here's a sample of my code that implements this approach using nested for loops.

sampleSize=300;
cell_array=676;
trans_matrix_size=2*cell_size;

mat_A=zeros(trans_matrix_size,trans_matrix_size);
mat_B=zeros(trans_matrix_size,trans_matrix_size);
mat_C=zeros(trans_matrix_size,trans_matrix_size);
mat_D=zeros(trans_matrix_size,trans_matrix_size);

nx1=0;
nx2=0;
ny1=ny2=ny3=ny4=0;
  
for x=1:1:sampleSize
    for mrow=1:1:trans_matrix_size
        if(mrow <= cell_size)
            nx1=nx1+1;
        else
            nx2=nx2+1;
        end
        for mcol=1:1:trans_matrix_size
            if(mcol <= cell_size && mrow <= cell_size)
                ny1=ny1+1;
                ny3=0;
                mat_A(mrow,mcol)=TA{nx1,ny1}(x); % TA, cell_size x cell_size x 300
                mat_B(mrow,mcol)=TA{nx1,ny1}(x); % identity matrix
                mat_C(mrow,mcol)=TA{nx1,ny1}(x);
            elseif(mcol <= cell_size && mrow > cell_size)
                ny2=ny2+1;
                ny4=0;
                mat_A(mrow,mcol)=TC{nx2,ny2}(x); % TC, cell_size x cell_size x 300
                mat_B(mrow,mcol)=Y{nx2,ny2}(x); % zero matrix
                mat_C(mrow,mcol)=TC{nx2,ny2}(x);
            elseif(mcol > cell_size && mrow <= cell_size)
                ny3=ny3+1;
                ny1=0;
                mat_A(mrow,mcol)=TB{nx1,ny3}(x);
                mat_B(mrow,mcol)=TC{nx1,ny3}(x);
                mat_C(mrow,mcol)=TB{nx1,ny3}(x);
            elseif(mcol > cell_size && mrow > cell_size)
                ny4=ny4+1;
                ny2=0;
                mat_A(mrow,mcol)=TA{nx2,ny4}(x);
                mat_B(mrow,mcol)=TA{nx2,ny4}(x);
                mat_C(mrow,mcol)=TA{nx2,ny4}(x);
            end
        end
    end
    mat_D(mrow,mcol)=mat_A*mat_B*mat_C;
end

Thanks for your help...

Subject: Avoiding Nested For loops

From: Rune Allnor

Date: 22 Dec, 2011 03:55:24

Message: 2 of 4

On 22 Des, 03:13, "Gary " <un4gettab...@hotmail.com> wrote:
> Hi,
>
> I have been working on trying to improve my CPU time.  I generated a set of data and have arranged them in such a way where I have three large matrices that are NxNx300 sample points.  I implemented this by using cell arrays.  When I run my code, I notice that it takes 6 hours at least to execute one section of my code and that is due to nested for loops that I have.  I would like to find a way to fetch data one at a time for
> 3 huge cell array matrices (T1, T2 and T3) that are NxNx300 and perform the following multiplication T_tot=T1*T2*T3 so that my result is T_tot is NxNx300.

This doesn't make sense. A 'matrix', in this context, is
specifically a 2D array where matrix products are well
defined. 3D arrays are something else:

- They are not 'matrices', use a different term.
- Products are not well-defined, you need to explain
  *exactly* what it is you attempt to do.

Whatever it is you attempt to do, will not become easier
by using the wrong terminology when you describe it.

Rune

Subject: Avoiding Nested For loops

From: Gary

Date: 22 Dec, 2011 04:34:09

Message: 3 of 4

Rune Allnor <allnor@tele.ntnu.no> wrote in message <201fa69c-ccab-4659-8c27-265522b17119@v14g2000yqh.googlegroups.com>...
> On 22 Des, 03:13, "Gary " <un4gettab...@hotmail.com> wrote:
> > Hi,
> >
> > I have been working on trying to improve my CPU time.  I generated a set of data and have arranged them in such a way where I have three large matrices that are NxNx300 sample points.  I implemented this by using cell arrays.  When I run my code, I notice that it takes 6 hours at least to execute one section of my code and that is due to nested for loops that I have.  I would like to find a way to fetch data one at a time for
> > 3 huge cell array matrices (T1, T2 and T3) that are NxNx300 and perform the following multiplication T_tot=T1*T2*T3 so that my result is T_tot is NxNx300.
>
> This doesn't make sense. A 'matrix', in this context, is
> specifically a 2D array where matrix products are well
> defined. 3D arrays are something else:
>
> - They are not 'matrices', use a different term.
> - Products are not well-defined, you need to explain
> *exactly* what it is you attempt to do.
>
> Whatever it is you attempt to do, will not become easier
> by using the wrong terminology when you describe it.
>
> Rune

Ok,

What I am ultimately trying to do is perform a multiplication of three matrices. Let me give another example. Let's say I have data that has already been generated and stored as a 3D array with the following size: #row=100, #col=100. The elements inside the 100x100 matrix is a vector with 300 sample points --> 100x100x300. This would make it a 3D array. So if I have three 3D arrays: M1, M2 and M3 that are 100x100x300 each in size. How can I avoid using nested for loops which will take hours to run to multiply: M=M1*M2*M3.
(for the record my actual 3D array is 1352x1352x300 in size.)

Subject: Avoiding Nested For loops

From: Rune Allnor

Date: 22 Dec, 2011 05:06:04

Message: 4 of 4

On 22 Des, 05:34, "Gary " <un4gettab...@hotmail.com> wrote:

> What I am ultimately trying to do is perform a multiplication of three matrices.  Let me give another example.  Let's say I have data that has already been generated and stored as a 3D array with the following size: #row=100, #col=100.  The elements inside the 100x100 matrix is a vector with 300 sample points --> 100x100x300.  This would make it a 3D array.  So if I have three 3D arrays:  M1, M2 and M3 that are 100x100x300 each in size.  How can I avoid using nested for loops which will take hours to run to multiply:  M=M1*M2*M3.
> (for the record my actual 3D array is 1352x1352x300 in size.)

You still haven't defined what you mean by
'multiply 3D arrays'.

Apart from that, you ask the wrong question.
The question is not how to avoid for-loops,
but how to implement these loops efficiently.

But no such questions can be answered before
you have defined what you actually want to do.

Rune

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
cell arrays Gary 21 Dec, 2011 21:14:10
nested for loops Gary 21 Dec, 2011 21:14:10
speed up Gary 21 Dec, 2011 21:14:10
matrix multipli... Gary 21 Dec, 2011 21:14:10
rssFeed for this Thread

Contact us at files@mathworks.com