필터 지우기
필터 지우기

replace nonzero element of matrix with vector

조회 수: 1 (최근 30일)
fatema saba
fatema saba 2015년 11월 19일
편집: Stephen23 2015년 11월 19일
Hello I have matrix A:
A=[2 3 0 0;6 0 2 4;1 2 0 7]
I want to replace nonzero element of this matrix with matrix B respectively. matrix B is:
B=[70;11;111;8;7;22;15;64]
Thank you.
  댓글 수: 2
fatema saba
fatema saba 2015년 11월 19일
Hi I found it. and write it for others.
fn=find(A~=0)
A(fn)=B
Stephen23
Stephen23 2015년 11월 19일
편집: Stephen23 2015년 11월 19일
@fatema saba: you do not need to use find, it just makes the code slower and more complicated without any benefit. It is better to use logical indexing, as per Walter Roberson's answer.

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

답변 (1개)

Walter Roberson
Walter Roberson 2015년 11월 19일
A(logical(A)) = B;
or if you prefer
A(A ~= 0) = B;

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by