0

I have the following logic operation coded in MATLAB, where [A, B, C, and D] are all 5x3x16 doubles and [a, b, c, and d] are all 240x1 doubles. I am trying to implement the same logic operation in python using numpy.

D = zeros(size(A)); 
for i = 1:numel(D)
    flag = ...
        (a == A(i)) & ...
        (b == B(i)) & ...
        (c == C(i));
    D(i) = d(flag);
end

d is a column vector that is already populated with data. a, b, and c are also populated column vectors of equal size. Meshgrid was used to construct A, B, and C into a LxMxN grid of the unique values within a, b, and c. Now I want to use d to populate a LxMxN D with the appropriate values using the boolean expression.

I have tried:

D= np.zeros(np.shape(N))
for i in range(len(D)):

    for j in range(len(D[0])):

        for k in range(len(D[0][0])):    
            flag = np.logical_and(                                  
                (a == A[i][j][k]),              
                (b == B[i][j][k]),        
                (c == C[i][j][k])
                )
            D[i][j][k] = d[flag];
MGoforth
  • 1
  • 2
  • Possible duplicate of [How to perform element wise boolean operations on numpy arrays](https://stackoverflow.com/questions/8632033/how-to-perform-element-wise-boolean-operations-on-numpy-arrays) – Cris Luengo Mar 21 '19 at 17:28
  • Welcome to the site. Please make sure to provide (cut down) sample data and target output. This will make it a lot easier to help. – mhhollomon Mar 21 '19 at 17:45
  • I'm really not clear on what you want `D` to be at the end of this. Can you provide some example data (e.g. A, B, and C, as 2x2x2 and a, b, c, and d as 3x1) with what you'd like/expect the output to be? – aganders3 Mar 21 '19 at 22:51
  • @aganders3 It is hard to give an example but I will try to explain. d is a column vector that is already populated with data. a, b, and c are also populated column vectors of equal size. Meshgrid was used to construct A, B, and C into a LxMxN grid of the unique values within a, b, and c. Now I want to use d to populate a LxMxN D with the appropriate values using the boolean expression. – MGoforth Mar 22 '19 at 21:14

1 Answers1

1

The syntax will be a little messier, but you can use the np.logical_* functions to do this.

aganders3
  • 5,433
  • 23
  • 27