简体   繁体   English

在Matlab中从3个3D数组创建3D向量数组

[英]Creating an array of 3D vectors from 3 3D arrays in matlab

Say I have three 3D arrays of size 2x2x2 假设我有3个大小为2x2x2的3D阵列

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

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

w =[[1 4][9 0];[4 5][3 1]] 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 我想创建一个大小为2x2x2的3d数组,将其存储为3D向量,其中元素从数组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)]] 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? 有没有一种方法可以在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 ? PS:我也希望这种表示形式具有像渐变这样的计算能力? 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: 这样做会很麻烦,而使用普通的matlab 3D矩阵则更直接:

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: 您可以使用cat命令将第四个维度上的内容简单地连接起来,如下所示:

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 然后,您感兴趣的3D向量位于V的最后维度,例如,您可以通过(1,2,1)获得向量

V(1,2,1,:)

or 要么

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

ans =

    4
    4
    4

if you want to get a 3x1 vector. 如果您想获得3x1向量。

If you must you can get matrices from the cell arrays using cell2mat, and get them in the right dimensions using reshape. 如果必须的话,您可以使用cell2mat从单元阵列中获取矩阵,并使用整形以正确的尺寸获取它们。 Check the matlab documentation for these: 有关以下内容,请查阅matlab文档:

doc cell2mat
doc reshape

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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