```Path: news.mathworks.com!not-for-mail
From: "Justin " <justinbobo1618@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: for loop slows dramatically with successive iterations
Date: Fri, 11 May 2012 21:44:12 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 39
Message-ID: <jok17c\$lm4\$1@newscl01ah.mathworks.com>
References: <jojqje\$nc4\$1@newscl01ah.mathworks.com> <jojv7t\$e5t\$1@newscl01ah.mathworks.com>
NNTP-Posting-Host: www-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1336772652 22212 172.30.248.47 (11 May 2012 21:44:12 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 11 May 2012 21:44:12 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:767586

Ah, it all makes sense now. Thanks for pointing out the problem in my code.

Before I read your response, I had an epiphany and I completely rewrote the function and it works beautifully now. Coincidentally, I ended up using pretty similar strategy to your suggestion. I wish I'd asked sooner--in the future, I'm coming straight here for help before I struggle for hours.

Here's my new function, much cleaner than my old one--only 4 lines of code. And I could easily make it 1 line, I just kept it like this for readability.

% convert vector into a matrix
y_reshape = reshape(y_bits,M_repeat,length(y_bits)/M_repeat);
% each column is M_repeat rows and represents the repeated values

y_sum = sum(y_reshape);
% y_sum is now a vector where each element is the sum of the corresponding column of y_reshape

% divide each element by the number of repeats to get an average value
% should be between 0 and 1 inclusive
y_norm = y_sum./M_repeat;

% majority rule means just round the average value to a 0 or 1
z_bits = round(y_norm);

"Roger Stafford" wrote in message <jojv7t\$e5t\$1@newscl01ah.mathworks.com>...
> "Justin " <justinbobo1618@gmail.com> wrote in message <jojqje\$nc4\$1@newscl01ah.mathworks.com>...
> >       z_bits(N*M_repeat-(N-1)) = 1;
> - - - - - - - - - -
>   Your problem comes with the line
>
>  z_bits(N*M_repeat-(N-1)) = 1;
>