# Rearrange element in vector

1

Suppose i have

v1 = { x1, x2, x3, x4, x5, x6 }

v2 = { x5,x3,x1}

How to rearrange V2 according to position in V1 so that

v3 = { x1,x3,x5}

0

There may be a better way to accomplish this, but you can:

```v1 = { x1, x2, x3, x4, x5 };
v2 = { x5, x3, x1 };

//Get the index of each element of v2's location in v1
idx = indcv(v2, v1);

//Sort the indices
idx = sortc(idx, 1);

//Assign v3
v3 = v1[idx];
```

This code will assign 'v3' to be:

```x1
x3
x5
```

You could accomplish the same action for numeric data by using the function indnv instead of indcv.

0

what if i want v4 to be

x1

.

x3

.

x5

0
```//Make v4 a missing value
v4 = miss(0,0);

//Reshape v4 into a 5x1 vector of missing values
v4 = reshape(v4, 5, 1);

//Use the same idx from the problem above
v4[idx] = v1[idx];
```

Then the print statement:

```print \$v4;
```

will return:

```X1
.
X3
.
X5
`
```

For reference, below is the complete code snippet for both parts:

```v1 = { x1, x2, x3, x4, x5 };
v2 = { x5, x3, x1 };

idx = indcv(v2, v1);

//Sort the indices
idx = sortc(idx, 1);

//Assign v3
v3 = v1[idx];

//Make v4 a missing value
v4 = miss(0,0);

//Reshape v4 into a 5x1 vector of missing values
v4 = reshape(v4, 5, 1);

v4[idx] = v1[idx];
```

Tags: asked November 4, 2012

0

There may be a better way to accomplish this, but you can:

```v1 = { x1, x2, x3, x4, x5 };
v2 = { x5, x3, x1 };

//Get the index of each element of v2's location in v1
idx = indcv(v2, v1);

//Sort the indices
idx = sortc(idx, 1);

//Assign v3
v3 = v1[idx];
```

This code will assign 'v3' to be:

```x1
x3
x5
```

You could accomplish the same action for numeric data by using the function indnv instead of indcv.

0

what if i want v4 to be

x1

.

x3

.

x5

0
```//Make v4 a missing value
v4 = miss(0,0);

//Reshape v4 into a 5x1 vector of missing values
v4 = reshape(v4, 5, 1);

//Use the same idx from the problem above
v4[idx] = v1[idx];
```

Then the print statement:

```print \$v4;
```

will return:

```X1
.
X3
.
X5
`
```

For reference, below is the complete code snippet for both parts:

```v1 = { x1, x2, x3, x4, x5 };
v2 = { x5, x3, x1 };

idx = indcv(v2, v1);

//Sort the indices
idx = sortc(idx, 1);

//Assign v3
v3 = v1[idx];

//Make v4 a missing value
v4 = miss(0,0);

//Reshape v4 into a 5x1 vector of missing values
v4 = reshape(v4, 5, 1);

v4[idx] = v1[idx];
```

• ### Aptech Systems, Inc. Worldwide Headquarters

Aptech Systems, Inc.
2350 East Germann Road, Suite #21
Chandler, AZ 85286

Phone: 360.886.7100
FAX: 360.886.8922

Ready to Get Started?

• ### Training & Events

Want more guidance while learning about the full functionality of GAUSS and its capabilities? Get in touch for in-person training or browse additional references below.

• ### Tutorials

Step-by-step, informative lessons for those who want to dive into GAUSS and achieve their goals, fast.

• ### Want to find out more?

Get a real answer from a real person

• ### Have a Specific Question?

Get a real answer from a real person

• Need Support?
• ### Support Plans

Premier Support and Platinum Premier Support are annually renewable membership programs that provide you with important benefits including technical support, product maintenance, and substantial cost-saving features for your GAUSS System or the GAUSS Engine.

• ### User Forums

Join our community to see why our users are considered some of the most active and helpful in the industry!

• ### Where to Buy

Available across the globe, you can have access to GAUSS no matter where you are.