Code:
. mata : // Define matrices : A = (1 , 2 , . \ 4 , 5 , 6 \ . , 8 , 9) : B = (3 , 4 , 7 \ 2 , . , . \ 1 , 8 , 2) : C = (1 , 2 , 7 \ 2 , 5 , 6 \ 1 , 8 , 2) : : // Matrix A : A 1 2 3 +-------------+ 1 | 1 2 . | 2 | 4 5 6 | 3 | . 8 9 | +-------------+ : // Matrix B : B 1 2 3 +-------------+ 1 | 3 4 7 | 2 | 2 . . | 3 | 1 8 2 | +-------------+ : // Desired Matrix C : C 1 2 3 +-------------+ 1 | 1 2 7 | 2 | 2 5 6 | 3 | 1 8 2 | +-------------+ : end
Code:
. mata : C = A : for (i=1; i<=3; i++) { > for (j=1; j<=3; j++) { > if (B[i,j] < A[i,j]) (C[i,j] = B[i,j]) > } > } : C 1 2 3 +-------------+ 1 | 1 2 7 | 2 | 2 5 6 | 3 | 1 8 2 | +-------------+ : end
Code:
. mata : C = J(3,3,.) : C = A:* (A:<B) + B:* (B:<=A) : C 1 2 3 +-------------+ 1 | 1 2 . | 2 | 2 . . | 3 | . 8 2 | +-------------+ : end
I'm looking for an approach that would be computationally efficent (since as mentioned above I'm working with large matrices), and would return the desired result even when encountering missing values. Would appreciate any advice.
Nir.
0 Response to Creating a matrix containing the minimum element-by-element value from other matrices
Post a Comment