2

I have two multi-dimensional arrays and I would like to evaluate and assign values for those matching and non-matching values.

array_1 = array([[1,2,2], [1,2,3],[1,2,1]])
array_2 = array([[1,1,2], [2,2,3],[3,1,1]])

The idea is that for each member of array 1, if the value is the same, I want to assign 0 and it doesn't, I want to assign a different value. The logic is something like this.

(array_1 ==1 and array_2 ==1) = 0
(array_1 ==1 and array_2 ==2) = 10 
(array_1 ==1 and array_2 ==3) = 15
(array_1 ==2 and array_2 ==1) = 5
(array_1 ==2 and array_2 ==2) = 0
(array_1 ==2 and array_2 ==3) = 15
(array_1 ==3 and array_2 ==1) = 5
(array_1 ==3 and array_2 ==2) = 10
(array_1 ==3 and array_3 ==3) = 0

The over all goal is to detect a change. A brief description of this methodology is available on
https://www.e-education.psu.edu/geog883/node/496

2 Answers 2

3

First: I am not familiar with the array format that you showed in your post. I have never seen an array instantiated in Python using just square brackets. That's not a function call.

Second: Your problem may not be fully specified. However: if you have shown us all the possible values that you can have in your input arrays, then only 1, 2, and 3 are possible. In that case, there are only 9 possible pairings of elements from array_1 and array_2, and the values you want as outputs can easily be stored in a 3 x 3 lookup table.

Finally: I use Numpy. You seem to want arrays, and Numpy is very common and handles arrays well.

import numpy as np
from itertools import product

arr1 = np.array([[1,2,2],[1,2,3],[1,2,1]], dtype=int)
arr2 = np.array([[1,1,2],[2,2,3],[3,1,1]], dtype=int)

lookup = np.array(((0,10,15),(5,0,15),(5,10,0)), dtype=int)
result = np.zeros_like(arr1)
for r, c in product(*[range(x) for x in arr1.shape]):
    a, b = arr1[r,c], arr2[r,c]
    result[r,c] = lookup[a-1,b-1]

print(arr1, "\n")
print(arr2, "\n")
print(result)

Here's the output:

[[1 2 2]
 [1 2 3]
 [1 2 1]] 

[[1 1 2]
 [2 2 3]
 [3 1 1]] 

[[ 0  5  0]
 [10  0  0]
 [15  5  0]]
Sign up to request clarification or add additional context in comments.

2 Comments

Yep... you're right. I misplaced the open bracket. There are 9 possibilities three of which are 0 (no change) and the others are assigned based on the type of change. Thanks for your help!
In hindsight, I offer a minor improvement. If you are willing to encode your conditions in arr1 and arr2 starting from zero rather than from one, then the lookup is simpler: just use result[r,c] = lookup[a,b]. I'm also wondering whether Numpy's fancy indexing can be brought to bear, eliminating both the need for zeros_like() and the for loop.
0

You can do this in a list comprehension:

Use a conditional expression to produce your values.

You put the if, else statement first and then iterate through to replace the values.

I have created a list and pulled a number at random from that list.

import random

array_1 = [[1, 2, 2], [1, 2, 3], [1, 2, 1]]
array_2 = [[1, 1, 2], [2, 2, 3], [3, 1, 1]]

lst = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

res = [[random.choice(lst) if i != j else 0 for i, j in zip(a, b)]
    for a, b in zip(array_1, array_2)]

for i in res:
    print(i)

Returns:

[0, 25, 0]
[50, 0, 0]
[35, 45, 0]

2 Comments

This would do the job but the idea was to print out one 3 x3 array.
I did it both ways as i was not sure.although it was the same lol. I have update my answer for one 3x3 matrix..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.