简体   繁体   中英

Creating an array of 3D vectors from 3 3D arrays in matlab

Say I have three 3D arrays of size 2x2x2

u =[[3 4][9 8];[1 2][3 4]]

v =[[5 4][8 5];[3 2][-1 4]]

w =[[1 4][9 0];[4 5][3 1]]

I want to create a single 3d array of size 2x2x2 that stores these as a 3D vector where the elements are derived from the arrays u,v,w

V = [[(3,5,1)(4,4,4)][(9,8,9)(8,5,0)];[(1,3,4)(2,2,5)][(3,-1,3)(4,4,1)]]

Is there a way to specify and do this in matlab?

EDIT: I changed the representation to avoid any confusion about cell arrays. They are all numeric arrays.

PS: I would also like this representation to have the capability of calculations like gradient and such ? Is that possible ?

Did you mean cell arrays like this?

u ={[3 4],[9 8];[1 2],[3 4]}
v ={[5 4],[8 5];[3 2],[-1 4]}
w ={[1 4],[9 0];[4 5],[3 1]} % (note the commas)

It would be very cumbersome to do it like this and it is much more straight-forward to use normal matlab 3D matrices like this:

u = cat(3,[3 4; 9 8],[1 2; 3 4])
v = cat(3,[5 4; 8 5],[3 2; -1 4])
w = cat(3,[1 4; 9 0],[4 5; 3 1])

You can simply concatenate those along the fourth dimension using the cat command like this:

V = cat(4, u, v, w)

The 3D vectors you are interested in are then in the last dimension of V, for example you can obtain the vector at (1,2,1) with

V(1,2,1,:)

or

>> squeeze(V(1,2,1,:))

ans =

    4
    4
    4

if you want to get a 3x1 vector.

If you must you can get matrices from the cell arrays using cell2mat, and get them in the right dimensions using reshape. Check the matlab documentation for these:

doc cell2mat
doc reshape

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM