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:
How can I make this loop faster?

Subject: How can I make this loop faster?

From: aykut

Date: 3 Nov, 2010 13:58:24

Message: 1 of 5

Hi experts,

I have a loop which I need to run many times. Therefore, I am looking for a faster solution, if possible. Basically, I have a matrix and I need to write it down in a file as formatted. every increment of i value, texta takes the values from the matrix and adds it at the end of itself. I think string adding by using [a, b] is a bit slow solution for that. Could you please give me a hand?

texta='';
for i=1:4:b
    texta=[texta, sprintf('%d\n%d %d %d\n%d %d %d\n%d %d %d\n%d %d %d\n',(i-1)/4+1,TempMotionVect(1,i), TempMotionVect(2,i),TempMotionVect(3,i),TempMotionVect(1,i+1), TempMotionVect(2,i+1),TempMotionVect(3,i+1),TempMotionVect(1,i+2), TempMotionVect(2,i+2),TempMotionVect(3,i+2),TempMotionVect(1,i+3), TempMotionVect(2,i+3),TempMotionVect(3,i+3))];
end

Subject: How can I make this loop faster?

From: Sean

Date: 3 Nov, 2010 14:15:06

Message: 2 of 5

aykut <aykut_avci@hotmail.com> wrote in message <1284740095.251010.1288792734046.JavaMail.root@gallium.mathforum.org>...
> Hi experts,
>
> I have a loop which I need to run many times. Therefore, I am looking for a faster solution, if possible. Basically, I have a matrix and I need to write it down in a file as formatted. every increment of i value, texta takes the values from the matrix and adds it at the end of itself. I think string adding by using [a, b] is a bit slow solution for that. Could you please give me a hand?
>
> texta='';
> for i=1:4:b
> texta=[texta, sprintf('%d\n%d %d %d\n%d %d %d\n%d %d %d\n%d %d %d\n',(i-1)/4+1,TempMotionVect(1,i), TempMotionVect(2,i),TempMotionVect(3,i),TempMotionVect(1,i+1), TempMotionVect(2,i+1),TempMotionVect(3,i+1),TempMotionVect(1,i+2), TempMotionVect(2,i+2),TempMotionVect(3,i+2),TempMotionVect(1,i+3), TempMotionVect(2,i+3),TempMotionVect(3,i+3))];
> end

preallocate a cell array:
C = cell(length(1:4:b),1)
on each loop iteration right your long thing to it
for jj = 1:length(1:4:b)
  ii = jj*4+1
C{jj} = your long expression(ii)
end

Then unpack your cell, C as necessary.

Subject: How can I make this loop faster?

From: Sean

Date: 3 Nov, 2010 14:40:06

Message: 3 of 5

should be:
> ii = (jj-1)*4+1

Subject: How can I make this loop faster?

From: aykut

Date: 4 Nov, 2010 09:57:54

Message: 4 of 5

Hi Sean,

Thank you very much for your reply.
I did what you said and works great. However, what I really wanted to ask was that , I believe, it is not a good idea to use 'sprintf' since I am formatting the values from again a matrix. I will write the output in a text file. Therefore, I need to convert values from first matrix to long output string. I thought there would be an easier and much faster way to do it.

thanks anyway.

Subject: How can I make this loop faster?

From: Jan Simon

Date: 4 Nov, 2010 11:14:05

Message: 5 of 5

Dear Aykut,

> However, what I really wanted to ask was that , I believe, it is not a good idea to use 'sprintf' since I am formatting the values from again a matrix.

In the shown loop, the string [texta] grows in each iteration. This is inefficient in Matlab (and any other programming language). Can you create just one string in each iteration and write it immediately to the disk using FPRINTF?

Jan

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