# Thread Subject: How to reduce these loops into a line

 Subject: How to reduce these loops into a line From: Elnaz Date: 15 May, 2012 21:31:20 Message: 1 of 5 Hi all, Since loops elongate the run time, I was thinking how can I reduce these two loops and instead write them in a line using vector indexing perhaps. noisycode=[];     for j = 1:111         for i=1:111             noisycode = [noisycode, r(96+5*i , 96+5*j)];         end     end The thing is that order is important here; I want to sweep the "r" matrix in a column-wise manner i.e. read column by column. Do you guys have any suggestion of how to do it? Thanks, Elnaz
 Subject: How to reduce these loops into a line From: Idin Motedayen-Aval Date: 15 May, 2012 22:37:30 Message: 2 of 5 On 5/15/2012 5:31 PM, Elnaz wrote: > Hi all, > Since loops elongate the run time, I was thinking how can I reduce these > two loops and instead write them in a line using vector indexing perhaps. > noisycode=[]; > for j = 1:111 > for i=1:111 > noisycode = [noisycode, r(96+5*i , 96+5*j)]; > end end > > The thing is that order is important here; I want to sweep the "r" > matrix in a column-wise manner i.e. read column by column. Do you guys > have any suggestion of how to do it? > > Thanks, > Elnaz This looks like a reshape. Just use the RESHAPE function. Pick out the section of r you need, then reshape it into a row vector. Something like this: n2 = reshape( r(96+5:5:96+555,96+5:5:96+555), 1, 111*111); -- Idin Motedayen-Aval The MathWorks, Inc. zq=[4 2 5 -15 -1 -3 24 -57 45 -12 19 -12 15 -8 3 -7 8 -69 53 12 -2]; char(filter(1,[1,-1],[105 zq])), clear zq
 Subject: How to reduce these loops into a line From: Idin Motedayen-Aval Date: 15 May, 2012 22:40:45 Message: 3 of 5 On 5/15/2012 5:31 PM, Elnaz wrote: > Hi all, > Since loops elongate the run time, I was thinking how can I reduce these > two loops and instead write them in a line using vector indexing perhaps. > noisycode=[]; > for j = 1:111 > for i=1:111 > noisycode = [noisycode, r(96+5*i , 96+5*j)]; > end end > > The thing is that order is important here; I want to sweep the "r" > matrix in a column-wise manner i.e. read column by column. Do you guys > have any suggestion of how to do it? > > Thanks, > Elnaz I should also note that the killer in your code isn't so much the FOR loops, but the fact that you're growing the noisycode vector inside the loop. This code will be much more efficient: noisycode=zeros(111,111); % pre-allocate your array for j = 1:111      for i=1:111          noisycode(i,j) = r(96+5*i , 96+5*j);      end end -- Idin Motedayen-Aval The MathWorks, Inc. zq=[4 2 5 -15 -1 -3 24 -57 45 -12 19 -12 15 -8 3 -7 8 -69 53 12 -2]; char(filter(1,[1,-1],[105 zq])), clear zq
 Subject: How to reduce these loops into a line From: Elnaz Date: 15 May, 2012 23:42:06 Message: 4 of 5 That was greatly helpful; thanks. What if I want to add 4 columns and rows of zeros between each element of a matrix just to separate the elements from each other with zeros in between, like this: for i = 1:111     for j = 1:111         bits(5*i-4,5*j-4) = image(i,j);     end end Can I do this without loops? Elnaz
 Subject: How to reduce these loops into a line From: Elnaz Date: 15 May, 2012 23:47:07 Message: 5 of 5 "Elnaz " wrote in message ... > That was greatly helpful; thanks. > What if I want to add 4 columns and rows of zeros between each element of a matrix just to separate the elements from each other with zeros in between, like this: > > for i = 1:111 > for j = 1:111 > bits(5*i-4,5*j-4) = image(i,j); > end > end > > Can I do this without loops? > > Elnaz I have my own answer: bits = zeros(551,551); bits(1:5:551,1:5:551) = image;

Separated by commas
Ex.: root locus, bode

### 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.