## Replacing values within a row of matrix with a vector?

### connor (view profile)

on 10 Oct 2013
Latest activity Commented on by Jos (10584)

on 10 Oct 2013

### Wayne King (view profile)

Basicly I have a matrix with only 0 and 1's.

```       a = [ 1 0 1 0;...
1 1 0 1;...
0 1 1 0;...
1 1 0 0];```

And i want to replace the 1's in the first row with the vector

`       b = [2 5]`

the number of 1's i want to replace will always be the same size as the vector b.

Is there any method for doing this??

Thanks

## Products

No products are associated with this question.

### Wayne King (view profile)

on 10 Oct 2013

There are a number of ways

```   a = [ 1 0 1 0;...
1 1 0 1;...
0 1 1 0;...
1 1 0 0];
b = [2 5];
idx = find(a(1,:) == 1);
a(1,idx) = b;```

connor

on 10 Oct 2013

Thanks!

Jos (10584)

### Jos (10584) (view profile)

on 10 Oct 2013

You can remove the find and use logical indexing

```tf = a(1,:)==1
a(1,tf) = b
```

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

Apply Today

New to MATLAB?

Learn MATLAB today!