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:
for loop but vectorize code

Subject: for loop but vectorize code

From: amira ali

Date: 24 Oct, 2012 16:44:09

Message: 1 of 5

plz need help to make for loop in one line as for loop take high processing time


sigma=5;
     guass= 1/(sqrt(2*pi)*sigma) * exp(-0.5*n2.^2/(sigma^2));
     %row =1:1:size(guass,1);
     z= size(guass,1);
     guass_new=[];
     for g=1:z
        x1=guass(g,:)./sum(guass);
        guass_new=vertcat(guass_new,x1);
         
     end

Subject: for loop but vectorize code

From: dpb

Date: 24 Oct, 2012 17:04:42

Message: 2 of 5

On 10/24/2012 11:44 AM, amira ali wrote:
> plz need help to make for loop in one line as for loop take high
> processing time
>
>
> sigma=5;
> guass= 1/(sqrt(2*pi)*sigma) * exp(-0.5*n2.^2/(sigma^2));
> z= size(guass,1);
> guass_new=[];
> for g=1:z
> x1=guass(g,:)./sum(guass);
> guass_new=vertcat(guass_new,x1);
> end

I suspect most of the time comes from the fact you didn't preallocate.
But, you don't need any loops anyway--sum(gauss) is constant so the ./
operator will do the same thing over the entire array as it does on a
row-by-row basis.

guass_new=guass./sum(guass);

--

Subject: for loop but vectorize code

From: amira ali

Date: 24 Oct, 2012 18:04:08

Message: 3 of 5

dpb <none@non.net> wrote in message <k69715$mcn$1@speranza.aioe.org>...
> On 10/24/2012 11:44 AM, amira ali wrote:
> > plz need help to make for loop in one line as for loop take high
> > processing time
> >
> >
> > sigma=5;
> > guass= 1/(sqrt(2*pi)*sigma) * exp(-0.5*n2.^2/(sigma^2));
> > z= size(guass,1);
> > guass_new=[];
> > for g=1:z
> > x1=guass(g,:)./sum(guass);
> > guass_new=vertcat(guass_new,x1);
> > end
>
> I suspect most of the time comes from the fact you didn't preallocate.
> But, you don't need any loops anyway--sum(gauss) is constant so the ./
> operator will do the same thing over the entire array as it does on a
> row-by-row basis.
>
> guass_new=guass./sum(guass);
>
> --
>
>
guass_new=guass./sum(guass); give me error as


sum(guass) dimenstion is 1*260 and guass dimensation is 1000*260, how can i solve this problem

Subject: for loop but vectorize code

From: amira ali

Date: 24 Oct, 2012 18:06:08

Message: 4 of 5

dpb <none@non.net> wrote in message <k69715$mcn$1@speranza.aioe.org>...
> On 10/24/2012 11:44 AM, amira ali wrote:
> > plz need help to make for loop in one line as for loop take high
> > processing time
> >
> >
> > sigma=5;
> > guass= 1/(sqrt(2*pi)*sigma) * exp(-0.5*n2.^2/(sigma^2));
> > z= size(guass,1);
> > guass_new=[];
> > for g=1:z
> > x1=guass(g,:)./sum(guass);
> > guass_new=vertcat(guass_new,x1);
> > end
>
> I suspect most of the time comes from the fact you didn't preallocate.
> But, you don't need any loops anyway--sum(gauss) is constant so the ./
> operator will do the same thing over the entire array as it does on a
> row-by-row basis.
>


give me error :??? Error using ==> rdivide
Matrix dimensions must agree.

AS SUM(GUASS) DIMMESTION IS 1*260 AND GUASS DIMENSTION IS 1000*260

HELP ME PLZ
> guass_new=guass./sum(guass);
>
> --
>
>

Subject: for loop but vectorize code

From: dpb

Date: 24 Oct, 2012 18:38:06

Message: 5 of 5

On 10/24/2012 1:04 PM, amira ali wrote:
...

>> I suspect most of the time comes from the fact you didn't preallocate.
>> But, you don't need any loops anyway--sum(gauss) is constant so the ./
>> operator will do the same thing over the entire array as it does on a
>> row-by-row basis.
>>
>> guass_new=guass./sum(guass);
>>
...

> guass_new=guass./sum(guass); give me error as
>
> sum(guass) dimenstion is 1*260 and guass dimensation is 1000*260...

Yeah, mental faux pas there, sorry.

One way...

guass_new=guass./repmat(sum(guass),size(gauss,1),1);

You can try that against the loop but preallocate instead of vertcat...

s=sum(g);
ng=zeros(size(g));
for i=1:size(g,1)
   ng(i,:)=g(i,:)./s;
end

--

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