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:
Make a matlab code faster

Subject: Make a matlab code faster

From: edward kabanyas

Date: 24 Jul, 2011 03:21:08

Message: 1 of 9

Hi All,

I make the following code:

[a, b] = size(tf);
cal_num = 0;
indx = 0;
for i=1:1:a
      
    if tf(i,1)==1 && tf(i,2) ==1 && tf(i,3)==1
            indx = indx +1;
            cal_num(indx)=i;
     end
            
        
end

It works well, but it is very slow. Do you have another option instead of using a loop process ?

Thanks for help.

Edward

Subject: Make a matlab code faster

From: TideMan

Date: 24 Jul, 2011 04:44:51

Message: 2 of 9

On Jul 24, 3:21 pm, "edward kabanyas" <djuky_...@yahoo.com> wrote:
> Hi All,
>
> I make the following code:
>
> [a, b] = size(tf);
> cal_num = 0;
> indx = 0;
> for i=1:1:a
>
>     if tf(i,1)==1 && tf(i,2) ==1 && tf(i,3)==1
>             indx = indx +1;
>             cal_num(indx)=i;
>      end
>
> end
>
> It works well, but it is very slow. Do you have another option instead of using a loop process ?
>
> Thanks for help.
>
> Edward

Instead of looping through each row, you can do it for all the rows
using find:
cal_num=find(tf==ones(a,b));

Subject: Make a matlab code faster

From: Roger Stafford

Date: 24 Jul, 2011 05:02:08

Message: 3 of 9

"edward kabanyas" <djuky_hmi@yahoo.com> wrote in message <j0g334$e5m$1@newscl01ah.mathworks.com>...
> [a, b] = size(tf);
> cal_num = 0;
> indx = 0;
> for i=1:1:a
> if tf(i,1)==1 && tf(i,2) ==1 && tf(i,3)==1
> indx = indx +1;
> cal_num(indx)=i;
> end
> end
>
> It works well, but it is very slow. Do you have another option instead of using a loop process ?
- - - - - - - - - -
 p = (1:size(tf,1)).';
 cal_num = p(all(tf(:,1:3)==1,2));

Roger Stafford

Subject: Make a matlab code faster

From: Bruno Luong

Date: 24 Jul, 2011 06:33:07

Message: 4 of 9

find(ismember(tf(:,1:3),[1 1 1],'rows'))

Bruno

Subject: Make a matlab code faster

From: edward kabanyas

Date: 24 Jul, 2011 23:34:08

Message: 5 of 9

Hi Tideman, Bruno and Roger..

Thanks for nice suugestion. The idea from Roger and Bruno work well for my case. Thanks again..

Edward

TideMan <mulgor@gmail.com> wrote in message <60a69f52-5999-4186-959f-80eec4201d89@r5g2000prf.googlegroups.com>...
> On Jul 24, 3:21 pm, "edward kabanyas" <djuky_...@yahoo.com> wrote:
> > Hi All,
> >
> > I make the following code:
> >
> > [a, b] = size(tf);
> > cal_num = 0;
> > indx = 0;
> > for i=1:1:a
> >
> >     if tf(i,1)==1 && tf(i,2) ==1 && tf(i,3)==1
> >             indx = indx +1;
> >             cal_num(indx)=i;
> >      end
> >
> > end
> >
> > It works well, but it is very slow. Do you have another option instead of using a loop process ?
> >
> > Thanks for help.
> >
> > Edward
>
> Instead of looping through each row, you can do it for all the rows
> using find:
> cal_num=find(tf==ones(a,b));

Subject: Make a matlab code faster

From: edward kabanyas

Date: 24 Jul, 2011 23:50:10

Message: 6 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j0geb3$a8n$1@newscl01ah.mathworks.com>...
> find(ismember(tf(:,1:3),[1 1 1],'rows'))
>
> Bruno

Hi Bruno & other friends,

Thanks for nice help. I try to check my code performance after replacing the above mentioned loop, it still takes long time and it is also due to another loop. This loop really makes my program take time

[xy, xz]= size(data);
data_cal =0;

for i=1:1:xy
    for j=1:1:xz
         cal_indx=find(CALSTD==data(i,j), 1);
         data_cal(i,j)=CALSTD(cal_indx,2);
    end
end

In this loop, I want to change the value in each point of data with the suitable value in CALSTD. CALSTD is a calibration table. The above code works well but it really slow. Could you provide me any faster solution and probably without loop !

Thanks again,
Edward
 

Subject: Make a matlab code faster

From: Roger Stafford

Date: 25 Jul, 2011 01:17:07

Message: 7 of 9

"edward kabanyas" <djuky_hmi@yahoo.com> wrote in message <j0ib3i$ilo$1@newscl01ah.mathworks.com>...
> ...... This loop really makes my program take time
>
> [xy, xz]= size(data);
> data_cal =0;
> for i=1:1:xy
> for j=1:1:xz
> cal_indx=find(CALSTD==data(i,j), 1);
> data_cal(i,j)=CALSTD(cal_indx,2);
> end
> end
>
> In this loop, I want to change the value in each point of data with the suitable value in CALSTD. CALSTD is a calibration table. The above code works well but it really slow. Could you provide me any faster solution and probably without loop !
>
> Thanks again,
> Edward
- - - - - - - - - - -
  Try this:

 CALSTD2 = flipud(CALSTD);
 [tf,loc] = ismember(data(:),CALSTD2(:,1));
 data_cal = zeros(size(data(:)));
 data_cal(tf) = CALSTD2(loc(tf),2);
 data_cal = reshape(data_cal,size(data));

  Note 1: I have reversed the row order in CALSTD to allow for the possibility that two elements in its first column are alike but the corresponding second column elements are different. Your 'find' would select the first of these and 'ismember' would select the second one.

  Note 2: As you have written your code, if the first column of CALSTD fails to have any element equal to a particular element of 'data' but does have an equal one in its second column, there should occur an error message because 'cal_indx' would then exceed the row index range. Remember, cal_indx is a linear index as it comes from 'find' but is used as a subscript in CALSTD(cal_indx,2).

Roger Stafford

Subject: Make a matlab code faster

From: edward kabanyas

Date: 25 Jul, 2011 01:51:07

Message: 8 of 9

Hi Roger,

It works, thanks so much !

Edward

"Roger Stafford" wrote in message <j0ig6j$t18$1@newscl01ah.mathworks.com>...
> "edward kabanyas" <djuky_hmi@yahoo.com> wrote in message <j0ib3i$ilo$1@newscl01ah.mathworks.com>...
> > ...... This loop really makes my program take time
> >
> > [xy, xz]= size(data);
> > data_cal =0;
> > for i=1:1:xy
> > for j=1:1:xz
> > cal_indx=find(CALSTD==data(i,j), 1);
> > data_cal(i,j)=CALSTD(cal_indx,2);
> > end
> > end
> >
> > In this loop, I want to change the value in each point of data with the suitable value in CALSTD. CALSTD is a calibration table. The above code works well but it really slow. Could you provide me any faster solution and probably without loop !
> >
> > Thanks again,
> > Edward
> - - - - - - - - - - -
> Try this:
>
> CALSTD2 = flipud(CALSTD);
> [tf,loc] = ismember(data(:),CALSTD2(:,1));
> data_cal = zeros(size(data(:)));
> data_cal(tf) = CALSTD2(loc(tf),2);
> data_cal = reshape(data_cal,size(data));
>
> Note 1: I have reversed the row order in CALSTD to allow for the possibility that two elements in its first column are alike but the corresponding second column elements are different. Your 'find' would select the first of these and 'ismember' would select the second one.
>
> Note 2: As you have written your code, if the first column of CALSTD fails to have any element equal to a particular element of 'data' but does have an equal one in its second column, there should occur an error message because 'cal_indx' would then exceed the row index range. Remember, cal_indx is a linear index as it comes from 'find' but is used as a subscript in CALSTD(cal_indx,2).
>
> Roger Stafford

Subject: Make a matlab code faster

From: Roger Stafford

Date: 25 Jul, 2011 20:15:12

Message: 9 of 9

"edward kabanyas" <djuky_hmi@yahoo.com> wrote in message <j0ii6b$3v8$1@newscl01ah.mathworks.com>...
> Hi Roger,
>
> It works, thanks so much !
>
> Edward
- - - - - - - - -
  You are quite welcome.

Roger Stafford

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