replace nonzero element of matrix with vector

2 次查看(过去 30 天)
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 个评论
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