Got Questions? Get Answers.
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:
Cellfun: subtraction of 2D cells with both dimensions ~=1

Subject: Cellfun: subtraction of 2D cells with both dimensions ~=1

From: Ashley

Date: 31 Jan, 2011 19:55:06

Message: 1 of 3

I have a for loop that I need to convert to cellfun, because the size is very large and it takes too long. This is an example of the for loop:

for j=1:length(a)
    for r=1:size(a,1)
        if isempty(a{r,j})~=1
            example{r,j}=-( blong{ length( b{1,j} ), j } - a{r,j} );
        end
    end
end

*blong equals b, but with the contents of the 1-row cells extracted to many rows of individual values (thus producing empty cells). I want subtract a{r,j} from the maximum value of b{:,j} (it's in ascending order). (and make it negative, of course)

I keep getting tripped up due to the size being n x n where neither n is equal to 1.

Subject: Cellfun: subtraction of 2D cells with both dimensions ~=1

From: Sean de

Date: 31 Jan, 2011 20:13:04

Message: 2 of 3

"Ashley" wrote in message <ii742q$e21$1@fred.mathworks.com>...
> I have a for loop that I need to convert to cellfun, because the size is very large and it takes too long. This is an example of the for loop:
>
> for j=1:length(a)
> for r=1:size(a,1)
> if isempty(a{r,j})~=1
> example{r,j}=-( blong{ length( b{1,j} ), j } - a{r,j} );
> end
> end
> end
>
> *blong equals b, but with the contents of the 1-row cells extracted to many rows of individual values (thus producing empty cells). I want subtract a{r,j} from the maximum value of b{:,j} (it's in ascending order). (and make it negative, of course)
>
> I keep getting tripped up due to the size being n x n where neither n is equal to 1.

cellfun and a for-loop are often comparable for speed. Is "example" preallocated? That is more than likely your time-sink.

example = cell(length(a),size(a,1));
 for j=1:length(a)
     for r=1:size(a,1)
         if ~isempty(a{r,j})
             example{r,j}=-( blong{ length( b{1,j} ), j } - a{r,j} );
         end
     end
 end

Good Luck!
%SCd
 

Subject: Cellfun: subtraction of 2D cells with both dimensions ~=1

From: Ashley

Date: 31 Jan, 2011 20:24:04

Message: 3 of 3

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ii754g$mg3$1@fred.mathworks.com>...
> "Ashley" wrote in message <ii742q$e21$1@fred.mathworks.com>...
> > I have a for loop that I need to convert to cellfun, because the size is very large and it takes too long. This is an example of the for loop:
> >
> > for j=1:length(a)
> > for r=1:size(a,1)
> > if isempty(a{r,j})~=1
> > example{r,j}=-( blong{ length( b{1,j} ), j } - a{r,j} );
> > end
> > end
> > end
> >
> > *blong equals b, but with the contents of the 1-row cells extracted to many rows of individual values (thus producing empty cells). I want subtract a{r,j} from the maximum value of b{:,j} (it's in ascending order). (and make it negative, of course)
> >
> > I keep getting tripped up due to the size being n x n where neither n is equal to 1.
>
> cellfun and a for-loop are often comparable for speed. Is "example" preallocated? That is more than likely your time-sink.
>
> example = cell(length(a),size(a,1));
> for j=1:length(a)
> for r=1:size(a,1)
> if ~isempty(a{r,j})
> example{r,j}=-( blong{ length( b{1,j} ), j } - a{r,j} );
> end
> end
> end
>
> Good Luck!
> %SCd
>


Thanks, Sean! I didn't realize such a huge difference that would make.

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