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:
split vector into 2 vectors according to a list of indices

Subject: split vector into 2 vectors according to a list of indices

From: Li Perry

Date: 16 Aug, 2010 12:40:10

Message: 1 of 4

Hi,

Let "Ind" be a vector of indices, and X some other vector.
I want to create 2 new vectors:
1. X(Ind)
2. X(rest of indices)

Is there an easy way to do this?

Thanks,
Li.

Subject: split vector into 2 vectors according to a list of indices

From: Wayne King

Date: 16 Aug, 2010 12:57:07

Message: 2 of 4

"Li Perry" <lazygoldenpanda@gmail.com> wrote in message <i4bbja$cbn$1@fred.mathworks.com>...
> Hi,
>
> Let "Ind" be a vector of indices, and X some other vector.
> I want to create 2 new vectors:
> 1. X(Ind)
> 2. X(rest of indices)
>
> Is there an easy way to do this?
>
> Thanks,
> Li.

Hi Li,

One way:

X = randn(10,1);
% generate some indices
 R = randi(10,4,1);
% get the indices of the complement
[C,~] = setdiff(1:length(X),R);
X1 = X(R);
X2 = X(C);

Wayne

Subject: split vector into 2 vectors according to a list of indices

From: Andy

Date: 16 Aug, 2010 13:01:26

Message: 3 of 4

"Li Perry" <lazygoldenpanda@gmail.com> wrote in message <i4bbja$cbn$1@fred.mathworks.com>...
> Hi,
>
> Let "Ind" be a vector of indices, and X some other vector.
> I want to create 2 new vectors:
> 1. X(Ind)
> 2. X(rest of indices)
>
> Is there an easy way to do this?
>
> Thanks,
> Li.

    X=1:20;
    ind = mod(X,2) == 0;
    Y = X(ind); % Y just has evens
    X(ind) = []; % cut evens out of X
    X,Y

    % displays:
    X =
         1 3 5 7 9 11 13 15 17 19

    Y =
         2 4 6 8 10 12 14 16 18 20

Subject: split vector into 2 vectors according to a list of indices

From: Li Perry

Date: 16 Aug, 2010 13:09:19

Message: 4 of 4

 Thanks Wayne, it's perfect :)

Tags for 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