0

I have the following arrays:

from mxnet import nd

A=nd.array([[1,1,1,1],[2,2,2,2]])
B=nd.array([[11,11,11,11],[22,22,22,22]])
Y=nd.array([[91,91,91,91],[92,92,92,92]])

Imagine that each list whithin each array corresponds to a client. So [1,1,1,1] is the result of operation A to client 1 and [2,2,2,2] is the result of operation A to client 2. Then I have another array with a diferent operation that is applied to all the clients. [11,11,11,11] is the result of operation B to client 1 and so on.

And I need to get the following result:

D=nd.array( [ [[1,1,1,1],[11,11,11,11]],[[2,2,2,2],[22,22,22,22]] ])
list([D,Y])

This returns:

[
 [[[ 1.  1.  1.  1.]
   [11. 11. 11. 11.]]

  [[ 2.  2.  2.  2.]
   [22. 22. 22. 22.]]]
 <NDArray 2x2x4 @cpu(0)>, 
 [[91. 91. 91. 91.]
  [92. 92. 92. 92.]]
 <NDArray 2x4 @cpu(0)>]

As you can see, the operations (A and B) are grouped for each client.

I tried:

list([list(zip(A,B)),Y])

And I get:

  [[(
    [1. 1. 1. 1.]
    <NDArray 4 @cpu(0)>, 
    [11. 11. 11. 11.]
    <NDArray 4 @cpu(0)>), (
    [2. 2. 2. 2.]
    <NDArray 4 @cpu(0)>, 
    [22. 22. 22. 22.]
    <NDArray 4 @cpu(0)>)], 
    [[91. 91. 91. 91.]
     [92. 92. 92. 92.]]
    <NDArray 2x4 @cpu(0)>]

Which is not what I need. Plus, arrays A and B are really big, so I don't want to use a loop or something that will take too long.

Thanks.

GabyLP
  • 3,649
  • 7
  • 45
  • 66
  • Something like list([nd.array(list(zip(list(A),list(B)))),Y]) ? To me it is not fully clear, what you are aiming at, I have to admit... – Sosel Aug 27 '19 at 14:08
  • @Sosel, thanks but I get an error "source_array must be array like object" – GabyLP Aug 27 '19 at 14:13

1 Answers1

0

this is typically an operation you can do with an mxnet.ndarray.concat, yet you need to expand the dimension of the concatenated items before the concat so that they stay in separate arrays. This command will get exactly the output you ask for:

C = nd.concat(A.expand_dims(axis=1), B.expand_dims(axis=1), dim=1)

print(C)

which returns:

[[[ 1.  1.  1.  1.]
  [11. 11. 11. 11.]]

 [[ 2.  2.  2.  2.]
  [22. 22. 22. 22.]]]
<NDArray 2x2x4 @cpu(0)>
Olivier Cruchant
  • 3,747
  • 15
  • 18