Reputation: 193
I have 3 vectors, named a, b and c and I want to create 3D matrix, M, so that M(i,j,k) = a(i) + b(j) + c(k), where a(i) means ith element of vector a, and likewise for all vectors and matrix.
For creating 2d matrix, it is easy like a+b'. but I am not sure how I can create 3d matrix.
Upvotes: 1
Views: 814
Reputation: 112759
You only need permute
or reshape
to do the multi-dimensional equivalent of transposition:
a + b.' + reshape(c, 1, 1, []);
Assuming that a
, b
, c
are column vectors of sizes L
×1
, M
×1
and N
×1
, this works because
a
is L
×1
, or equivalently L
×1
×1
;b.'
is 1
×M
×1
;reshape(c, 1, 1, []
is 1
×1
×N
.So, by implicit expansion the result is an L
×M
×N
3D array.
Upvotes: 4