http://www.mathworks.com/matlabcentral/newsreader/view_thread/323389
MATLAB Central Newsreader  Duplicating values in an array
Feed for thread: Duplicating values in an array
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 27 Sep 2012 01:22:07 +0000
Duplicating values in an array
http://www.mathworks.com/matlabcentral/newsreader/view_thread/323389#888241
Jac
I am faced with a larger problem, however here is a simplified example of my problem.<br>
<br>
If I have a vector A = [1 3 5 6]<br>
and this needs to be duplicated using a vector B = [2 2 2 3] (Doesn't neccessarily done using a vector, if you know another method) <br>
To result in:<br>
C = [1 1 3 3 5 5 6 6 6]<br>
<br>
The idea is simple enough so i assume this is a one line method that can do it that I am not aware of.<br>
<br>
If anyone knows how to do this there help would be greatly appreciated! Thanks!

Thu, 27 Sep 2012 04:41:08 +0000
Re: Duplicating values in an array
http://www.mathworks.com/matlabcentral/newsreader/view_thread/323389#888250
Roger Stafford
"Jac " <jac_domney@hotmail.com> wrote in message <k409nv$mk0$1@newscl01ah.mathworks.com>...<br>
> If I have a vector A = [1 3 5 6]<br>
> and this needs to be duplicated using a vector B = [2 2 2 3] (Doesn't neccessarily done using a vector, if you know another method) <br>
> To result in:<br>
> C = [1 1 3 3 5 5 6 6 6]<br>
         <br>
If you want to allow for the possibility of zero counts occurring in B, do this:<br>
<br>
p = cumsum(accumarray([1,cumsum(B)+1],1));<br>
C = A(p(1:end1));<br>
<br>
Roger Stafford

Fri, 28 Sep 2012 04:37:07 +0000
Re: Duplicating values in an array
http://www.mathworks.com/matlabcentral/newsreader/view_thread/323389#888343
Jac
Thanks for your help.<br>
<br>
However doing this results in the error:<br>
??? Subscript indices must either be real positive integers or logicals.<br>
<br>
Any suggestions, or guesses of what i have done wrong??

Fri, 28 Sep 2012 06:10:08 +0000
Re: Duplicating values in an array
http://www.mathworks.com/matlabcentral/newsreader/view_thread/323389#888350
Roger Stafford
"Jac " <jac_domney@hotmail.com> wrote in message <k439hj$sq2$1@newscl01ah.mathworks.com>...<br>
> However doing this results in the error:<br>
> ??? Subscript indices must either be real positive integers or logicals.<br>
> Any suggestions, or guesses of what i have done wrong??<br>
         <br>
It is assumed here that B is a row vector of nonnegative (exact!) integers and with no more elements than A contains. You can test your B and A for that with this logical proposition:<br>
<br>
size(B,1)==1 & all(B>=0) & all(B==round(B)) & numel(B)<=numel(A)<br>
<br>
which should be TRUE.<br>
<br>
If that turns out all right, I suggest you break up the code into equivalent elementary steps:<br>
<br>
t1 = cumsum(B);<br>
t2 = [1,t1+1];<br>
t3 = accumarray(t2,1);<br>
p = cumsum(t3);<br>
t4 = p(1:end1);<br>
C = A(t4);<br>
<br>
and see at which point you get an error message or an answer that doesn't seem correct. Hopefully that will guide you to the cause of the trouble.<br>
<br>
Roger Stafford

Fri, 28 Sep 2012 13:28:26 +0000
Re: Duplicating values in an array
http://www.mathworks.com/matlabcentral/newsreader/view_thread/323389#888377
Steven_Lord
<br>
<br>
"Jac " <jac_domney@hotmail.com> wrote in message <br>
news:k439hj$sq2$1@newscl01ah.mathworks.com...<br>
> Thanks for your help.<br>
><br>
> However doing this results in the error:<br>
> ??? Subscript indices must either be real positive integers or logicals.<br>
><br>
> Any suggestions, or guesses of what i have done wrong??<br>
<br>
Generally that error means one of two things.<br>
<br>
1) You're trying to do something like:<br>
<br>
x = 1:10;<br>
y = x(0);<br>
<br>
MATLAB uses 1based indexing, not 0based indexing.<br>
<br>
2) You're trying to call a function but you previously defined a variable <br>
with that same name.<br>
<br>
plot = 17;<br>
% Snip many lines of code, perhaps<br>
x = 0:0.1:2*pi;<br>
y = sin(x);<br>
plot(x, y)<br>
<br>
While you intended this to call the PLOT function to display the sine curve, <br>
instead it attempts to index into the variable named plot.<br>
<br>
Set a breakpoint on the line where this occurs and use WHOS to determine if <br>
you're in the second case.<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>