[Tutor] Subtract a 1D Array from a 2D Array

Steven D'Aprano steve at pearwood.info
Sat May 13 11:33:36 EDT 2017


On Sat, May 13, 2017 at 09:33:19AM -0400, Stephen P. Molnar wrote:

> I have two arrays:
> 
>         X           Y          Z
>     	            a	
>         0	        0	       0
>      2.059801	    0	       0
>     -1.203126	 3.402953	   0
>     -1.108639	-1.567853	 2.715601
>     -0.938564	-1.32733	-2.299003
>     	           a_c	
>     0.4283375	 0.91755	 0.208299

I'm having trouble interpreting what that means. What are X, Y, Z? Are 
they part of the array? Which array? Why is there a lone "a" in the 
middle of row 2, and a lone "a_c" in the middle of row 8?

Can you give some Python code that creates the two arrays? And then show 
us what result you expect to get?

Since the actual values in the array are not important, please use 
SIMPLE values. We shouldn't have to do detailed mental arithmetic 
with seven-significant figures to understand what you are trying to 
do. You should use nice simple single-digit (or at most two-digit) 
values for your examples.

My *guess* is that you have an array called "a" with 3 columns and 5 
rows, and a second array called "a_m" with 3 columns and 1 row, and you 
want to subtract a_m from each row of a.


a = [[ 9  8  7 ]
     [ 5  5  5 ]
     [ 4  4  4 ]
     [ 3  3  3 ]
     [ 2  2  2 ]]

a_m = [[ 1  2  3 ]]

And you want the result of a - a_m to be:

[[ 8  6  4 ]
 [ 4  3  2 ]
 [ 3  2  1 ]
 [ 2  1  0 ]
 [ 1  0 -1 ]]


Am I close? If this is not what you want, please explain what you 
actually do want.




-- 
Steve


More information about the Tutor mailing list