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 with big array

Subject: for loop with big array

From: Patricia Rosales

Date: 21 Apr, 2010 12:55:19

Message: 1 of 5

Hello,

I am trying to concatenate an array of 650240 elements (in binary), by consecutive pairs, in order to obtain the decimal number that represents each pair. The obtained number is stored in the Y array.
Obviously the order i concatenate the elements is very important, that is why i haven´t used the reshape function.
But this takes around 30 minutes. Someone could give me a hint to solve this?

Thank you!


Y=[];
R=fread(fid,elements,'uint8',0,mformat)';
Z=dec2bin(cast(R,'uint32'),8);
 
for n = 1:2:elements-1
        Y=[Y;bin2dec([Z(n,:),Z(n+1,:)])];
end

Subject: for loop with big array

From: Steve Amphlett

Date: 21 Apr, 2010 13:05:21

Message: 2 of 5

"Patricia Rosales" <proslop@oc.mde.es> wrote in message <hqmsjn$8hf$1@fred.mathworks.com>...
> Hello,
>
> I am trying to concatenate an array of 650240 elements (in binary), by consecutive pairs, in order to obtain the decimal number that represents each pair. The obtained number is stored in the Y array.
> Obviously the order i concatenate the elements is very important, that is why i haven´t used the reshape function.
> But this takes around 30 minutes. Someone could give me a hint to solve this?
>
> Thank you!
>
>
> Y=[];
> R=fread(fid,elements,'uint8',0,mformat)';
> Z=dec2bin(cast(R,'uint32'),8);
>
> for n = 1:2:elements-1
> Y=[Y;bin2dec([Z(n,:),Z(n+1,:)])];
> end

Preallocate your Y array. Then fill it in the loop rather than growing it.

Subject: for loop with big array

From: Sean

Date: 21 Apr, 2010 13:05:22

Message: 3 of 5

"Patricia Rosales" <proslop@oc.mde.es> wrote in message <hqmsjn$8hf$1@fred.mathworks.com>...
> Hello,
>
> I am trying to concatenate an array of 650240 elements (in binary), by consecutive pairs, in order to obtain the decimal number that represents each pair. The obtained number is stored in the Y array.
> Obviously the order i concatenate the elements is very important, that is why i haven´t used the reshape function.
> But this takes around 30 minutes. Someone could give me a hint to solve this?
>
> Thank you!
>
>
> Y=[];
> R=fread(fid,elements,'uint8',0,mformat)';
> Z=dec2bin(cast(R,'uint32'),8);
>
> for n = 1:2:elements-1
> Y=[Y;bin2dec([Z(n,:),Z(n+1,:)])];
> end

Y is changing on every loop iteration and this is your time kill. Preallocate Y:
Y = zeros(size(Z));

Subject: for loop with big array

From: Patricia Rosales

Date: 21 Apr, 2010 13:37:07

Message: 4 of 5

It worked, thank you!

"Patricia Rosales" <proslop@oc.mde.es> wrote in message <hqmsjn$8hf$1@fred.mathworks.com>...
> Hello,
>
> I am trying to concatenate an array of 650240 elements (in binary), by consecutive pairs, in order to obtain the decimal number that represents each pair. The obtained number is stored in the Y array.
> Obviously the order i concatenate the elements is very important, that is why i haven´t used the reshape function.
> But this takes around 30 minutes. Someone could give me a hint to solve this?
>
> Thank you!
>
>
> Y=[];
> R=fread(fid,elements,'uint8',0,mformat)';
> Z=dec2bin(cast(R,'uint32'),8);
>
> for n = 1:2:elements-1
> Y=[Y;bin2dec([Z(n,:),Z(n+1,:)])];
> end

Subject: for loop with big array

From: Patricia Rosales

Date: 21 Apr, 2010 13:38:04

Message: 5 of 5

It worked, thank you very much!

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <hqmt6i$iq8$1@fred.mathworks.com>...
> "Patricia Rosales" <proslop@oc.mde.es> wrote in message <hqmsjn$8hf$1@fred.mathworks.com>...
> > Hello,
> >
> > I am trying to concatenate an array of 650240 elements (in binary), by consecutive pairs, in order to obtain the decimal number that represents each pair. The obtained number is stored in the Y array.
> > Obviously the order i concatenate the elements is very important, that is why i haven´t used the reshape function.
> > But this takes around 30 minutes. Someone could give me a hint to solve this?
> >
> > Thank you!
> >
> >
> > Y=[];
> > R=fread(fid,elements,'uint8',0,mformat)';
> > Z=dec2bin(cast(R,'uint32'),8);
> >
> > for n = 1:2:elements-1
> > Y=[Y;bin2dec([Z(n,:),Z(n+1,:)])];
> > end
>
> Y is changing on every loop iteration and this is your time kill. Preallocate Y:
> Y = zeros(size(Z));

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