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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.