想象一下一个2x2x2三向数据立方体:
data = [1 2; 3 4];
data(:,:,2) = [5 6; 7 8]
我希望从这个立方体(即,2x2矩阵)生成行-列切片,其中切片的每个元素是通过随机采样其3模光纤(即,第n模光纤是沿第n模/维/路运行的向量)来获得的。这个立方体中有4根3模光纤,其中一根是f1 =1 5,另一根是f2 =2 6,以此类推。例如,一个切片可能会变成:
slice = [5 2; 3 4]
不同的采样可能会导致切片:
slice = [1 2; 7 8]
有没有一种快速的方法可以做到这一点?
我尝试使用slice = datasample(data,1,3),但此函数从多维数据集中随机选取一个行-列切片(即,slice =1 2;3 4或5 6;7 8)。
发布于 2013-10-25 04:54:25
在任何模式(例如,nmode=3
是3模式)上尝试此解决方案:
data = cat(3,[1 2; 3 4],[5 6; 7 8]);
nmode = 3;
Dn = size(data,nmode);
modeSampleInds = randi(Dn,1,numel(data)/Dn); % here is the random sample
dp = reshape(permute(data,[nmode setdiff(1:ndims(data),nmode)]), Dn, []);
outSize = size(data); outSize(nmode)=1;
slice = reshape(dp(sub2ind(size(dp),modeSampleInds,1:size(dp,2))),outSize)
请注意,这不需要统计工具箱。对于任何矩阵大小、维数和“N模光纤”,它也是完全通用的。
如果需要的话,我很乐意解释每一行。
发布于 2013-10-25 04:54:45
如果我没理解错的话,那实际上是很简单的。您有四个3模光纤,您希望构建一个2x2矩阵,其中每个元素都是从相应的光纤中采样的。
因此,您需要采样4次(每个纤程一次)2个元素中的一个(每个纤程有两个元素):
>> [h w fiberSize] = size(data); % make the solution more general
>> fIdx = randsample( fiberSize, h*w ); % sample with replacements
采样之后,我们构建切片,为了简单起见,我将3D数据“展平”成2D矩阵
>> fData = reshape( data, [], fiberSize );
>> slice = fData( sub2ind( [h*w fiberSize], 1:(h*w), fIdx ) );
>> slice = reshape( slice, [h w] ); % shape the slice
发布于 2013-10-25 05:24:37
以下解决方案适用于任何多维数据集大小。不需要工具箱。
N = size(data,1); %//length of side of cube
r = randi(N,1,N^2)-1; %//this is the random sampling
data_permuted = permute(data,[3 1 2]); %//permute so that sampling is along first dim
slice = data_permuted((1:N:N^3)+r); %//sample using linear indexing
slice = reshape(slice.',N,N); %//reshape into a matrix
https://stackoverflow.com/questions/19573800
复制相似问题