Matrix Matlab Assignment

I have a question about the purpose of the matrix.

let's say I have three matrices A, B and C, and I want to assign the elements of the matrix C to the elements A and B according to the rule

C[i,j] = A[i,j] if abs(C[i,j] - A[i,j]) < abs(C[i,j] - B[i,j]) C[i,j] = B[i,j] if abs(C[i,j] - A[i,j]) > abs(C[i,j] - B[i,j]) C[i,j] = 0 if abs(C[i,j] - A[i,j]) == abs(C[i,j] - B[i,j]) 

how can i write it without loops?

Many thanks for your help.

+4
source share
2 answers

I think Dan Becker has the right idea, but recalculating abs(CB) and abs(CA) implies that the updated matrices are compared, not the original ones.

I don’t think this is what you want, so the version of his method is fixed here:

 CmA = abs(CA); CmB = abs(CB); ind = Cma < CmB; C(ind) = A(ind); ind = CmA > CmB; C(ind) = B(ind); C(CmA == CmB) = 0; 
+5
source

I think you want the following:

 ind = abs(C - A) < abs(C - B) ; C(ind) = A(ind); ind = abs(C - A) > abs(C - B) ; C(ind) = B(ind); ind = abs(C - A) == abs(C - B) ; C(ind) = 0; 
+1
source

Source: https://habr.com/ru/post/1442896/


All Articles