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:
matlab version and "for" loops

Subject: matlab version and "for" loops

From: Jessica

Date: 20 Jan, 2013 22:12:08

Message: 1 of 16

I am currently running Matlab 7.10 (R2010a) on a Mac. I heard that the newer versions of Matlab may be able to run "for" loops considerably faster and was wondering whether this is true or not. And if it is the case, does anyone know approximately how much faster the loops run?

Subject: matlab version and "for" loops

From: Jan Simon

Date: 21 Jan, 2013 09:26:08

Message: 2 of 16

Dear Jessica,

Yes, "newer" versions of Matlab interpret loops much faster using the JIT acceleration. Please note that this means "newer" than version 6.1 from June 2001.

To check the difference type:

  feature('accel', 'off')
  feature('jit', 'off')

and run your program again.

If you want to accelerate your code, post it here to get our ideas.

Kind regards, Jan

Subject: matlab version and "for" loops

From: Bruno Luong

Date: 21 Jan, 2013 09:53:07

Message: 3 of 16

"Jessica" wrote in message <kdhq3o$bcl$1@newscl01ah.mathworks.com>...
> I am currently running Matlab 7.10 (R2010a) on a Mac. I heard that the newer versions of Matlab may be able to run "for" loops considerably faster and was wondering whether this is true or not. And if it is the case, does anyone know approximately how much faster the loops run?

IIRC, 2010a already has major improvement of for-loop. I works on PC, but I believe the improvement comes across all platforms.

Bruno

Subject: matlab version and "for" loops

From: Jessica

Date: 21 Jan, 2013 23:16:07

Message: 4 of 16

"Jan Simon" wrote in message <kdj1jg$i5i$1@newscl01ah.mathworks.com>...
> Dear Jessica,
>
> Yes, "newer" versions of Matlab interpret loops much faster using the JIT acceleration. Please note that this means "newer" than version 6.1 from June 2001.
>
> To check the difference type:
>
> feature('accel', 'off')
> feature('jit', 'off')
>
> and run your program again.
>
> If you want to accelerate your code, post it here to get our ideas.
>
> Kind regards, Jan

Thanks for this information. My problem is in running this code:

%Delete saccades except when they mark the start/stop of image
DataLength=size(Sdata);DataLength=DataLength(1);
for i=0:DataLength-1
    if strcmp(GazeType(DataLength-i,1),'Saccade')==1|strcmp(GazeType(DataLength- i,1),'Unclassified')==1;
        if strcmp(PictureStartColumn2(DataLength-i),'ImageStart')==0
            if strcmp(PictureStartColumn2(DataLength-i),'ImageEnd')==0;
                Stextdata2(DataLength-i,:)=[];
                Sdata(DataLength-i,:)=[];
            end;
        end;
    end;
end;
end;


The loop often needs to run over 300,000 times (i.e., DataLength=300,000). After nearly 8 hours , I stopped the code to find that it was only up to 53,988.

I am basically trying to delete rows in a dataset that are marked as "Saccade" or "Unclassified" unless they are marked as "ImageStart" or "ImageEnd"

Subject: matlab version and "for" loops

From: Bruno Luong

Date: 21 Jan, 2013 23:53:07

Message: 5 of 16

"Jessica" wrote in message <kdki7n$b6g$1@newscl01ah.mathworks.com>...
> "Jan Simon" wrote in message <kdj1jg$i5i$1@newscl01ah.mathworks.com>...
> > Dear Jessica,
> >
> > Yes, "newer" versions of Matlab interpret loops much faster using the JIT acceleration. Please note that this means "newer" than version 6.1 from June 2001.
> >
> > To check the difference type:
> >
> > feature('accel', 'off')
> > feature('jit', 'off')
> >
> > and run your program again.
> >
> > If you want to accelerate your code, post it here to get our ideas.
> >
> > Kind regards, Jan
>
> Thanks for this information. My problem is in running this code:
>
> %Delete saccades except when they mark the start/stop of image
> DataLength=size(Sdata);DataLength=DataLength(1);
> for i=0:DataLength-1
> if strcmp(GazeType(DataLength-i,1),'Saccade')==1|strcmp(GazeType(DataLength- i,1),'Unclassified')==1;
> if strcmp(PictureStartColumn2(DataLength-i),'ImageStart')==0
> if strcmp(PictureStartColumn2(DataLength-i),'ImageEnd')==0;
> Stextdata2(DataLength-i,:)=[];
> Sdata(DataLength-i,:)=[];
> end;
> end;
> end;
> end;
> end;
>

First, it's not for-loop that is slow. Thing that is slow is calling strcmp many times.
strcmp can work on cell of strings. You should use strcmp and logical indexing to remove unwanted data. No need for-loop at all.

Bruno

Subject: matlab version and "for" loops

From: Jessica

Date: 22 Jan, 2013 09:55:08

Message: 6 of 16

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdkkd3$hvs$1@newscl01ah.mathworks.com>...
> "Jessica" wrote in message <kdki7n$b6g$1@newscl01ah.mathworks.com>...
> > "Jan Simon" wrote in message <kdj1jg$i5i$1@newscl01ah.mathworks.com>...
> > > Dear Jessica,
> > >
> > > Yes, "newer" versions of Matlab interpret loops much faster using the JIT acceleration. Please note that this means "newer" than version 6.1 from June 2001.
> > >
> > > To check the difference type:
> > >
> > > feature('accel', 'off')
> > > feature('jit', 'off')
> > >
> > > and run your program again.
> > >
> > > If you want to accelerate your code, post it here to get our ideas.
> > >
> > > Kind regards, Jan
> >
> > Thanks for this information. My problem is in running this code:
> >
> > %Delete saccades except when they mark the start/stop of image
> > DataLength=size(Sdata);DataLength=DataLength(1);
> > for i=0:DataLength-1
> > if strcmp(GazeType(DataLength-i,1),'Saccade')==1|strcmp(GazeType(DataLength- i,1),'Unclassified')==1;
> > if strcmp(PictureStartColumn2(DataLength-i),'ImageStart')==0
> > if strcmp(PictureStartColumn2(DataLength-i),'ImageEnd')==0;
> > Stextdata2(DataLength-i,:)=[];
> > Sdata(DataLength-i,:)=[];
> > end;
> > end;
> > end;
> > end;
> > end;
> >
>
> First, it's not for-loop that is slow. Thing that is slow is calling strcmp many times.
> strcmp can work on cell of strings. You should use strcmp and logical indexing to remove unwanted data. No need for-loop at all.
>
> Bruno

Thanks for looking my code over. Do you mind giving me a little more guidance on how I would make a logical index for this particular problem?

Subject: matlab version and "for" loops

From: Bruno Luong

Date: 22 Jan, 2013 11:30:08

Message: 7 of 16

Here is a simple example, feel free to adapt to your case:

GazeType={'Delete' 'Keep' 'Fruit' 'Delete' 'Benzen'}
Picture={'Image' 'Picasso' 'Matisse' 'Dali' 'You'}
Data = 1:5

delete_b = strcmp(GazeType,'Delete') & ~strcmp(Picture,'Image');
Data(delete) = []

% Bruno

Subject: matlab version and "for" loops

From: Jessica

Date: 22 Jan, 2013 19:52:09

Message: 8 of 16

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdlt80$st0$1@newscl01ah.mathworks.com>...
> Here is a simple example, feel free to adapt to your case:
>
> GazeType={'Delete' 'Keep' 'Fruit' 'Delete' 'Benzen'}
> Picture={'Image' 'Picasso' 'Matisse' 'Dali' 'You'}
> Data = 1:5
>
> delete_b = strcmp(GazeType,'Delete') & ~strcmp(Picture,'Image');
> Data(delete) = []
>
> % Bruno

This is much faster-- thanks for the tip!

Subject: matlab version and "for" loops

From: Jessica

Date: 26 Jan, 2013 22:50:08

Message: 9 of 16

"Jessica " <jly5@duke.edu> wrote in message <kdmql8$n4i$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdlt80$st0$1@newscl01ah.mathworks.com>...
> > Here is a simple example, feel free to adapt to your case:
> >
> > GazeType={'Delete' 'Keep' 'Fruit' 'Delete' 'Benzen'}
> > Picture={'Image' 'Picasso' 'Matisse' 'Dali' 'You'}
> > Data = 1:5
> >
> > delete_b = strcmp(GazeType,'Delete') & ~strcmp(Picture,'Image');
> > Data(delete) = []
> >
> > % Bruno
>
> This is much faster-- thanks for the tip!

And, one other question. If I want to do the same as above except delete based on numbers, how would I do that? If I do:

delete_b = GazeType(GazeType>10)

it only lists the numbers that are greater than 10 and I therefore cannot delete those rows.

Subject: matlab version and "for" loops

From: Gautam Sethi

Date: 27 Jan, 2013 03:22:48

Message: 10 of 16

On Saturday, January 26, 2013 5:50:08 PM UTC-5, Jessica wrote:
> "Jessica " <jly5@duke.edu> wrote in message <kdmql8$n4i$1@newscl01ah.mathworks.com>...
>
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdlt80$st0$1@newscl01ah.mathworks.com>...
>
> > > Here is a simple example, feel free to adapt to your case:
>
> > >
>
> > > GazeType={'Delete' 'Keep' 'Fruit' 'Delete' 'Benzen'}
>
> > > Picture={'Image' 'Picasso' 'Matisse' 'Dali' 'You'}
>
> > > Data = 1:5
>
> > >
>
> > > delete_b = strcmp(GazeType,'Delete') & ~strcmp(Picture,'Image');
>
> > > Data(delete) = []
>
> > >
>
> > > % Bruno
>
> >
>
> > This is much faster-- thanks for the tip!
>
>
>
> And, one other question. If I want to do the same as above except delete based on numbers, how would I do that? If I do:
>
>
>
> delete_b = GazeType(GazeType>10)
>
>
>
> it only lists the numbers that are greater than 10 and I therefore cannot delete those rows.

If GazeType is a column vector, you can do:

GazeType(GazeType > 10) = [];

Gautam.

Subject: matlab version and "for" loops

From: Gautam Sethi

Date: 27 Jan, 2013 03:27:09

Message: 11 of 16

On Saturday, January 26, 2013 10:22:48 PM UTC-5, Gautam Sethi wrote:
> On Saturday, January 26, 2013 5:50:08 PM UTC-5, Jessica wrote:
>
> > "Jessica " <jly5@duke.edu> wrote in message <kdmql8$n4i$1@newscl01ah.mathworks.com>...
>
> >
>
> > > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdlt80$st0$1@newscl01ah.mathworks.com>...
>
> >
>
> > > > Here is a simple example, feel free to adapt to your case:
>
> >
>
> > > >
>
> >
>
> > > > GazeType={'Delete' 'Keep' 'Fruit' 'Delete' 'Benzen'}
>
> >
>
> > > > Picture={'Image' 'Picasso' 'Matisse' 'Dali' 'You'}
>
> >
>
> > > > Data = 1:5
>
> >
>
> > > >
>
> >
>
> > > > delete_b = strcmp(GazeType,'Delete') & ~strcmp(Picture,'Image');
>
> >
>
> > > > Data(delete) = []
>
> >
>
> > > >
>
> >
>
> > > > % Bruno
>
> >
>
> > >
>
> >
>
> > > This is much faster-- thanks for the tip!
>
> >
>
> >
>
> >
>
> > And, one other question. If I want to do the same as above except delete based on numbers, how would I do that? If I do:
>
> >
>
> >
>
> >
>
> > delete_b = GazeType(GazeType>10)
>
> >
>
> >
>
> >
>
> > it only lists the numbers that are greater than 10 and I therefore cannot delete those rows.
>
>
>
> If GazeType is a column vector, you can do:
>
>
>
> GazeType(GazeType > 10) = [];
>
>
>
> Gautam.

Since you need to assign the new vector to delete_b, you can do

delete_b = GazeType; delete_b(GazeType > 10) = [];

Subject: matlab version and "for" loops

From: dpb

Date: 27 Jan, 2013 04:44:35

Message: 12 of 16

On 1/26/2013 4:50 PM, Jessica wrote:
> "Jessica " <jly5@duke.edu> wrote in message
> <kdmql8$n4i$1@newscl01ah.mathworks.com>...
>> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
>> <kdlt80$st0$1@newscl01ah.mathworks.com>...
...

>> > Data(delete) = []
...

> And, one other question. If I want to do the same as above except delete
> based on numbers, how would I do that? If I do:
>
> delete_b = GazeType(GazeType>10)
>
> it only lists the numbers that are greater than 10 and I therefore
> cannot delete those rows.

Generalize the rule Bruno illustrated--

GazeType(GazeType>10)=[];

--

Subject: matlab version and "for" loops

From: Jessica

Date: 27 Jan, 2013 13:55:08

Message: 13 of 16

dpb <none@non.net> wrote in message <ke2bbi$98i$1@speranza.aioe.org>...
> On 1/26/2013 4:50 PM, Jessica wrote:
> > "Jessica " <jly5@duke.edu> wrote in message
> > <kdmql8$n4i$1@newscl01ah.mathworks.com>...
> >> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> >> <kdlt80$st0$1@newscl01ah.mathworks.com>...
> ...
>
> >> > Data(delete) = []
> ...
>
> > And, one other question. If I want to do the same as above except delete
> > based on numbers, how would I do that? If I do:
> >
> > delete_b = GazeType(GazeType>10)
> >
> > it only lists the numbers that are greater than 10 and I therefore
> > cannot delete those rows.
>
> Generalize the rule Bruno illustrated--
>
> GazeType(GazeType>10)=[];
>
> --
>

Unfortunately, this won't quite work because I am trying to delete rows within larger data sets:


First, I isolate the column I am interested in:

RemoveColumn3=Total4(:, 11);

Then I wanted to generate a variable (just like I did above when the value was a string), that indicates the rows to be deleted (which are greater than 10).

And then, delete those specific rows within 2 datasets:
Total4(delete) = []
Target4(delete) = []

Subject: matlab version and "for" loops

From: Jessica

Date: 12 Feb, 2013 16:07:16

Message: 14 of 16

"Jessica " <jly5@duke.edu> wrote in message <ke3bjs$b6t$1@newscl01ah.mathworks.com>...
> dpb <none@non.net> wrote in message <ke2bbi$98i$1@speranza.aioe.org>...
> > On 1/26/2013 4:50 PM, Jessica wrote:
> > > "Jessica " <jly5@duke.edu> wrote in message
> > > <kdmql8$n4i$1@newscl01ah.mathworks.com>...
> > >> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> > >> <kdlt80$st0$1@newscl01ah.mathworks.com>...
> > ...
> >
> > >> > Data(delete) = []
> > ...
> >
> > > And, one other question. If I want to do the same as above except delete
> > > based on numbers, how would I do that? If I do:
> > >
> > > delete_b = GazeType(GazeType>10)
> > >
> > > it only lists the numbers that are greater than 10 and I therefore
> > > cannot delete those rows.
> >
> > Generalize the rule Bruno illustrated--
> >
> > GazeType(GazeType>10)=[];
> >
> > --
> >
>
> Unfortunately, this won't quite work because I am trying to delete rows within larger data sets:
>
>
> First, I isolate the column I am interested in:
>
> RemoveColumn3=Total4(:, 11);
>
> Then I wanted to generate a variable (just like I did above when the value was a string), that indicates the rows to be deleted (which are greater than 10).
>
> And then, delete those specific rows within 2 datasets:
> Total4(delete) = []
> Target4(delete) = []


I am having some trouble generalizing this logical method. Specifically, I am reading in a .csv file and trying to delete "Rows" that have even numbers:

Dataset=importdata('02-05-13_No11_WithHeaders.csv');
FinalDataSet=Dataset.data;
FinalDataSet2=Dataset.data;
Row=Dataset.data(:,5);
tf=bitget(abs(Row),1)~=0;
FinalDataSet2(tf)=[];

However, "FinalDataSet2" is originally sized as 81539x6. When I delete the "tf" values, it turns into a variable that is sized as 1x448465.

Can anyone suggest what I am doing incorrectly here?
Thanks!

Subject: matlab version and "for" loops

From: dpb

Date: 12 Feb, 2013 17:36:10

Message: 15 of 16

On 2/12/2013 10:07 AM, Jessica wrote:
> "Jessica " <jly5@duke.edu> wrote in message
> <ke3bjs$b6t$1@newscl01ah.mathworks.com>...
>> dpb <none@non.net> wrote in message <ke2bbi$98i$1@speranza.aioe.org>...
>> > On 1/26/2013 4:50 PM, Jessica wrote:
>> > > "Jessica " <jly5@duke.edu> wrote in message
>> > > <kdmql8$n4i$1@newscl01ah.mathworks.com>...
>> > >> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
>> > >> <kdlt80$st0$1@newscl01ah.mathworks.com>...
>> > ...
>> > > >> > Data(delete) = []
>> > ...
>> > > > And, one other question. If I want to do the same as above
>> except delete
>> > > based on numbers, how would I do that? If I do:
>> > >
>> > > delete_b = GazeType(GazeType>10)
>> > >
>> > > it only lists the numbers that are greater than 10 and I therefore
>> > > cannot delete those rows.
>> > > Generalize the rule Bruno illustrated--
>> > > GazeType(GazeType>10)=[];
>> > > --
>> >
>> Unfortunately, this won't quite work because I am trying to delete
>> rows within larger data sets:
>>
>>
>> First, I isolate the column I am interested in:
>>
>> RemoveColumn3=Total4(:, 11);
>>
>> Then I wanted to generate a variable (just like I did above when the
>> value was a string), that indicates the rows to be deleted (which are
>> greater than 10).
>>
>> And then, delete those specific rows within 2 datasets:
>> Total4(delete) = []
>> Target4(delete) = []
>
>
> I am having some trouble generalizing this logical method. Specifically,
> I am reading in a .csv file and trying to delete "Rows" that have even
> numbers:
>
> Dataset=importdata('02-05-13_No11_WithHeaders.csv');
> FinalDataSet=Dataset.data;
> FinalDataSet2=Dataset.data;
> Row=Dataset.data(:,5);
> tf=bitget(abs(Row),1)~=0;
> FinalDataSet2(tf)=[];
>
> However, "FinalDataSet2" is originally sized as 81539x6. When I delete
> the "tf" values, it turns into a variable that is sized as 1x448465.
>
> Can anyone suggest what I am doing incorrectly here?

Well, when you don't remove elements such that those being removed are
specified as either complete rows or columns the result can't be a
regularly spaced array and so ML returns a vector.

Given a (short) example dataset that illustrates the input and desired
results.

Reading the first part literally, to eliminate rows w/ even indices simply

x(2:2:end,:)=[];

does that job.

--

Subject: matlab version and "for" loops

From: Jessica

Date: 13 Feb, 2013 21:53:09

Message: 16 of 16

dpb <none@non.net> wrote in message <kfdui8$66t$1@speranza.aioe.org>...
> On 2/12/2013 10:07 AM, Jessica wrote:
> > "Jessica " <jly5@duke.edu> wrote in message
> > <ke3bjs$b6t$1@newscl01ah.mathworks.com>...
> >> dpb <none@non.net> wrote in message <ke2bbi$98i$1@speranza.aioe.org>...
> >> > On 1/26/2013 4:50 PM, Jessica wrote:
> >> > > "Jessica " <jly5@duke.edu> wrote in message
> >> > > <kdmql8$n4i$1@newscl01ah.mathworks.com>...
> >> > >> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> >> > >> <kdlt80$st0$1@newscl01ah.mathworks.com>...
> >> > ...
> >> > > >> > Data(delete) = []
> >> > ...
> >> > > > And, one other question. If I want to do the same as above
> >> except delete
> >> > > based on numbers, how would I do that? If I do:
> >> > >
> >> > > delete_b = GazeType(GazeType>10)
> >> > >
> >> > > it only lists the numbers that are greater than 10 and I therefore
> >> > > cannot delete those rows.
> >> > > Generalize the rule Bruno illustrated--
> >> > > GazeType(GazeType>10)=[];
> >> > > --
> >> >
> >> Unfortunately, this won't quite work because I am trying to delete
> >> rows within larger data sets:
> >>
> >>
> >> First, I isolate the column I am interested in:
> >>
> >> RemoveColumn3=Total4(:, 11);
> >>
> >> Then I wanted to generate a variable (just like I did above when the
> >> value was a string), that indicates the rows to be deleted (which are
> >> greater than 10).
> >>
> >> And then, delete those specific rows within 2 datasets:
> >> Total4(delete) = []
> >> Target4(delete) = []
> >
> >
> > I am having some trouble generalizing this logical method. Specifically,
> > I am reading in a .csv file and trying to delete "Rows" that have even
> > numbers:
> >
> > Dataset=importdata('02-05-13_No11_WithHeaders.csv');
> > FinalDataSet=Dataset.data;
> > FinalDataSet2=Dataset.data;
> > Row=Dataset.data(:,5);
> > tf=bitget(abs(Row),1)~=0;
> > FinalDataSet2(tf)=[];
> >
> > However, "FinalDataSet2" is originally sized as 81539x6. When I delete
> > the "tf" values, it turns into a variable that is sized as 1x448465.
> >
> > Can anyone suggest what I am doing incorrectly here?
>
> Well, when you don't remove elements such that those being removed are
> specified as either complete rows or columns the result can't be a
> regularly spaced array and so ML returns a vector.
>
> Given a (short) example dataset that illustrates the input and desired
> results.
>
> Reading the first part literally, to eliminate rows w/ even indices simply
>
> x(2:2:end,:)=[];
>
> does that job.
>
> --
That was a big help-- thanks for the tip!

Tags for this Thread

No tags are associated with 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