matlab的维纳滤波函数用法_matlab-张量的模n展开与折叠

问题提出背景

虽然matlab中已经有人开发出了相应的工具箱,但是我们依然可以借助matlab中已有的函数来完成张量的按模展开运算,而且在其他人的算法实现过程中也会经常遇到这种实现方式,记录下来,已示留念。

例子

以三模张量为例,可以推广到n模,没试过,遇到再说吧:

  • 构造张量
A

结果如下:

A(:,:,1) =

     1     9
     5     2


A(:,:,2) =

     6     3
    10     7


A(:,:,3) =

    11     8
     4    12

1,

模n展开
dim = size(A);
mode = ndims(A);
A1 = reshape(shiftdim(A,0), dim(1), [])%模1展开
A2 = reshape(shiftdim(A,1), dim(2), [])%模2展开
A3 = reshape(shiftdim(A,mode-1), dim(mode), [])%模3展开

结果:

dim =
     2     2     3

mode =
     3

A1 =
     1     9     6     3    11     8
     5     2    10     7     4    12

A2 =
     1     6    11     5    10     4
     9     3     8     2     7    12

A3 =
     1     5     9     2
     6    10     3     7
    11     4     8    12

给一张图便于理解

6e57d795384ba55d24efb4478d029019.png

可以看到,的确是按模展开的。

2,

模n折叠
dim1 

比较复杂,不好理解。

>> reshape(A2, dim2)

ans(:,:,1) =
     1     6    11
     9     3     8
ans(:,:,2) =
     5    10     4
     2     7    12

>> reshape(A3, dim3)

ans(:,:,1) =
     1     5
     6    10
    11     4
ans(:,:,2) =
     9     2
     3     7
     8    12

7406dbc8bab5d2a0cfd1d7ce7773f1f5.png

函数解析

reshape()可以改造数组,这个数组不仅仅是二维数组还可以是多维数组
  1. reshape(X,M,N) or reshape(X,[M,N]) returns the M-by-N matrix whose elements are taken columnwise from X. An error results if X does not have M*N elements.也就是说不管X是几维数组,最后都会返回 M by N 的矩阵。而且X中的元素的个数一定是M*N个,否则便会报错。
  2. reshape(X,M,N,P,...) or reshape(X,[M,N,P,...]) returns an N-D array with the same elements as X but reshaped to have the size M-by-N-by-P-by-.... The product of the specified dimensions, M*N*P*..., must be the same as NUMEL(X).同上理解
  3. reshape(X,...,[],...) calculates the length of the dimension represented by [], such that the product of the dimensions equals NUMEL(X). The value of NUMEL(X) must be evenly divisible by the product of the specified dimensions. You can use only one occurrence of [].简单理解为自动匹配维数

需要注意的是:如果使用第三种函数,那么必须严格按照参数列表来写,

>> A3 = reshape(shiftdim(A,mode-1), [dim(mode), []])%模3展开
错误使用 reshape
大小矢量必须包含至少两个元素。
 
>> A3 = reshape(shiftdim(A,mode-1), dim(mode), [])%模3展开
A3 =

     1     5     9     2
     6    10     3     7
    11     4     8    12

在做一个简单的测试,

>> B = pascal(4)
B =
     1     1     1     1
     1     2     3     4
     1     3     6    10
     1     4    10    20
>> reshape(B,[2,8])
ans =
     1     1     1     3     1     6     1    10
     1     1     2     4     3    10     4    20

>> reshape(B,2,8)
ans =
     1     1     1     3     1     6     1    10
     1     1     2     4     3    10     4    20

>> reshape(B,2,[])
ans =
     1     1     1     3     1     6     1    10
     1     1     2     4     3    10     4    20

>> reshape(B,[],8)
ans =
     1     1     1     3     1     6     1    10
     1     1     2     4     3    10     4    20

>> reshape(B,[],[])
错误使用 reshape
大小只能具有一个未知维度。
shiftdim()比较复杂,不展开来讲了,
shiftdim(A,1)时把第一个维度2拿出来,剩余的2*3左移,再把拿出的2放到最右边,就变成了2*3*2的矩阵;
shiftdim(A,2)时把前两个维度2*2拿出来,剩余的3左移,再把拿出的2*2放到最右边,就变成了3*2*2的矩阵。
关于shiftdim函数用法 - MATLAB中文论坛​www.ilovematlab.cn
ee1e4c117e9d8ab86fd7268de86bd465.png
circshift(),嗯,循环位移函数,用的不多,总结不出来,强行解释的话还不如直接看官方文档,请移步help circshift

简单来讲,

  1. dim1 =circshift(dim,[0,0]);dim1 = circshift(dim, [0, 0]);%不变
  2. dim2 = circshift(dim, [-1, -1]);%所有的行向上移动1行,所有的列向作移动1列
  3. dim3 = circshift(dim, [-2, -2]);%所有的行向上移动2行,所有的列向作移动2列

[dim1,dim2,dim3,...],这个数组中的第i个元素代表数组沿着第几个mode变化,正数和负数的含义取决于对matlab数组中mode的理解,也就是说多维数组A是 M by N by O by P.那么mode1就是所说的行,mode2就是所说的列,mode3就是所说的tube(管),mode4就没有对应的称呼了。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值