将1D向量(nx1)更改为3D矩阵(1x1xn) [英] Change 1D vector (nx1) to 3D matrix (1x1xn)

查看:162
本文介绍了将1D向量(nx1)更改为3D矩阵(1x1xn)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出a= [1;2;3],我想将其更改为b,其中b

Given a= [1;2;3] I want to change this to b where b is

b(1,1,1) = 1
b(1,1,2) = 2
b(1,1,3) = 3.

我该怎么做?没有内置命令吗?

How can I do this? Is there no built-in command for this?

推荐答案

使用 permute 将第一个尺寸最后作为第三个尺寸扔回去,并将第三个和第二个尺寸放在最前面(它们的顺序无关紧要).因此,我们将有两个这样的实现,就像这样-

Use permute to throw the first dimension back at the end as third dimension and bring the third and second dimensions to the front (their orders won't matter). Thus, we would have two such implementations, like so -

permute(a,[3 2 1])

permute(a,[2 3 1])

您还可以使用 reshape 来推回元素到第三维,就像这样-

You can also use reshape to push back the elements to the third dimension, like so -

reshape(a,1,1,numel(a))


关于permute


Little tutorial on permute

没有任何permute(尺寸重新布置)更改的3D阵列A将为: permute(A,[1 2 3]) .

A 3D array A without any permute(rearrangement of dimensions) changes would be : permute(A,[1 2 3]).

现在,您所做的任何permuting都将是w.r.t. [1 2 3]的原始顺序.假设您要交换第一维和第三维,因此交换[1 2 3]中的13,为我们提供[3 2 1]并将其用作permute中的第二个参数.

Now, any permuting you do, would be w.r.t. the original order of [1 2 3]. Let's say you want to swap 1st and 3rd dimensions, so swap the 1 and 3 in [1 2 3], giving us [3 2 1] and use it as the second argument in permute.

这是为了增强您的permuting技能-假设您交换了第一维和第三维,然后对这个排列的3D数组进行了一些处理.现在,您想恢复到原始顺序,因此需要调换第一维和第三维.因此,您再次使用[3,2,1],就像这样- permute(permute(A,[3 2 1]),[3 2 1]) ,这实际上是 permute(A,[1 2 3]) ,是的,是A,回到 home

Here's to make your permuting skills stronger - Let's say, you swap first and third dimensions and then you do some processing on this permuted 3D array. Now, you want to get back to the original order, so you need to swap back the 1st and 3rd dimensions. So, you use [3,2,1] again, like so - permute(permute(A,[3 2 1]),[3 2 1]) and this would be essentially permute(A,[1 2 3]) and yes that's A, back to home!

这篇关于将1D向量(nx1)更改为3D矩阵(1x1xn)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆