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:
Duplicating values in an array

Subject: Duplicating values in an array

From: Jac

Date: 27 Sep, 2012 01:22:07

Message: 1 of 5

I am faced with a larger problem, however here is a simplified example of my problem.

If I have a vector A = [1 3 5 6]
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)
To result in:
C = [1 1 3 3 5 5 6 6 6]

The idea is simple enough so i assume this is a one line method that can do it that I am not aware of.

If anyone knows how to do this there help would be greatly appreciated! Thanks!

Subject: Duplicating values in an array

From: Roger Stafford

Date: 27 Sep, 2012 04:41:08

Message: 2 of 5

"Jac " <jac_domney@hotmail.com> wrote in message <k409nv$mk0$1@newscl01ah.mathworks.com>...
> If I have a vector A = [1 3 5 6]
> 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)
> To result in:
> C = [1 1 3 3 5 5 6 6 6]
- - - - - - - - - -
  If you want to allow for the possibility of zero counts occurring in B, do this:

 p = cumsum(accumarray([1,cumsum(B)+1],1));
 C = A(p(1:end-1));

Roger Stafford

Subject: Duplicating values in an array

From: Jac

Date: 28 Sep, 2012 04:37:07

Message: 3 of 5

Thanks for your help.

However doing this results in the error:
??? Subscript indices must either be real positive integers or logicals.

Any suggestions, or guesses of what i have done wrong??

Subject: Duplicating values in an array

From: Roger Stafford

Date: 28 Sep, 2012 06:10:08

Message: 4 of 5

"Jac " <jac_domney@hotmail.com> wrote in message <k439hj$sq2$1@newscl01ah.mathworks.com>...
> However doing this results in the error:
> ??? Subscript indices must either be real positive integers or logicals.
> Any suggestions, or guesses of what i have done wrong??
- - - - - - - - - -
  It is assumed here that B is a row vector of non-negative (exact!) integers and with no more elements than A contains. You can test your B and A for that with this logical proposition:

  size(B,1)==1 & all(B>=0) & all(B==round(B)) & numel(B)<=numel(A)

which should be TRUE.

  If that turns out all right, I suggest you break up the code into equivalent elementary steps:

 t1 = cumsum(B);
 t2 = [1,t1+1];
 t3 = accumarray(t2,1);
 p = cumsum(t3);
 t4 = p(1:end-1);
 C = A(t4);

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.

Roger Stafford

Subject: Duplicating values in an array

From: Steven_Lord

Date: 28 Sep, 2012 13:28:26

Message: 5 of 5



"Jac " <jac_domney@hotmail.com> wrote in message
news:k439hj$sq2$1@newscl01ah.mathworks.com...
> Thanks for your help.
>
> However doing this results in the error:
> ??? Subscript indices must either be real positive integers or logicals.
>
> Any suggestions, or guesses of what i have done wrong??

Generally that error means one of two things.

1) You're trying to do something like:

x = 1:10;
y = x(0);

MATLAB uses 1-based indexing, not 0-based indexing.

2) You're trying to call a function but you previously defined a variable
with that same name.

plot = 17;
% Snip many lines of code, perhaps
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y)

While you intended this to call the PLOT function to display the sine curve,
instead it attempts to index into the variable named plot.

Set a breakpoint on the line where this occurs and use WHOS to determine if
you're in the second case.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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