hhh
hhh

Reputation: 52840

Matlab: how to pad ones to zero vector according to index vector?

I am trying to find a more elegant way to pad ones to zero vector according to the index (1,3) of the four-length vector. How can you do it more elegantly/succintly?

Input

(0,0,0,0) and (1,3)

Intended output

(1,0,1,0)

Trial

>> B=[0,1,0,0;0,1,0,1;1,0,0,0;1,1,1,0];

>> find(B(1,:)==0 & B(4,:)==1)

ans =

     1     3

>> zeros(1,4)+[1,0,1,0]

ans =

     1     0     1     0

Basically (1,3) ---> (1,0,1,0).

Upvotes: 2

Views: 78

Answers (1)

bla
bla

Reputation: 26069

If your input is I such that I=[0,0,0,0] and the index pair is ind=[1,3], then just

I(ind)=1;

This is a very basic matlab question, and I think just reading the documentation about matrix indexing should have sufficed.

Upvotes: 4

Related Questions