Hi all,
I have, on the face of it, a very simple question that has been causing me a great deal of trouble.
I’m working with two real matrices, A and B, that necessarily have the same dimensions. These matrices will not usually be square, although they will tend to have a similar number of rows and columns (typically between 1000 and 5000).
I’d like to replace the values in A with the corresponding value in B whenever A equals 0, leaving the other elements of A unchanged. The problem arises when B is missing, as I only want this missingness to propagate through to the resulting matrix if A is initially equal to 0 (or missing).
For instance, if we define:
Then the resulting matrix should be:
... which I am having difficulty arriving at it. My initial attempt had been to run:
But then elements in A, irrespective of their previous value, are replaced with missing whenever the corresponding element in B is missing.
Short of running loops across rows and columns, I am not sure how best to address this. Given the size of the matrices, I am keen to avoid any undue computational cost.
Any suggestions would be greatly appreciated.
Best,
Dylan
I have, on the face of it, a very simple question that has been causing me a great deal of trouble.
I’m working with two real matrices, A and B, that necessarily have the same dimensions. These matrices will not usually be square, although they will tend to have a similar number of rows and columns (typically between 1000 and 5000).
I’d like to replace the values in A with the corresponding value in B whenever A equals 0, leaving the other elements of A unchanged. The problem arises when B is missing, as I only want this missingness to propagate through to the resulting matrix if A is initially equal to 0 (or missing).
For instance, if we define:
Code:
A = (., 0 \ 0, 1)B = (0, . \ 1, .)
Code:
WANT = (., . \ 1, 1)
Code:
A = (A :!= 0) :* A :+ (A :== 0) :* B
Short of running loops across rows and columns, I am not sure how best to address this. Given the size of the matrices, I am keen to avoid any undue computational cost.
Any suggestions would be greatly appreciated.
Best,
Dylan

Comment