Comparing two matrices, and acting upon the result element by element.

1 回表示 (過去 30 日間)
Matt
Matt 2011 年 12 月 11 日
Hi All,
I have two large 2D matrices, A and B of equal dimensions, containing integer values.
What I would like to do is if the value of the element in A is equal to a specific value, then use the value from B, but otherwise leave the value of A unchanged. I am pretty certain this should be simple to implement but I have hit a brick wall in my competence.
Thank you very much!
M
(I have found a way of doing this with a dreadfully inefficient bash script, but this takes literally days to run, as it involves lots of temporary files etc. There are also ways I have fond in previous answers that come close to solving it, but I can't find anything specific enough that I can successfully adapt it.)

採用された回答

Paulo Silva
Paulo Silva 2011 年 12 月 11 日
A=randi(5,3,3)
B=10*randi(5,3,3)+5
v=2 %your specific value
A(A==v)=B(A==v) %replace elements of A equal to v by B ele. on same position
  3 件のコメント
Paulo Silva
Paulo Silva 2011 年 12 月 11 日
nice catch, silly mistake, thanks :)
Matt
Matt 2011 年 12 月 12 日
Perfect, thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by