Replacing values within a row of matrix with a vector?

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

 채택된 답변

Wayne King
Wayne King 2013년 10월 10일
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;

댓글 수: 2

Thanks!
You can remove the find and use logical indexing
tf = a(1,:)==1
a(1,tf) = b

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

질문:

2013년 10월 10일

댓글:

2013년 10월 10일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by