我的R脚本加载了一个4维数据集,这是一个三维医学图像的时间序列。我使用时间序列创建一个掩码,用于排除在每个时间点值为0的体素(三维像素):
voxels[is.na(voxels)]=0; # just get rid of unusable data
mask=rowSums(voxels,dims=3); # 3D image that is the sum over time
mask=(mask!=0); # make binary因此,要在掩码内的索引中找到每个时间点的值,我想要这样做:
indices=which(mask!=0); # find the positions of nonzeroes in the mask
voxelsfound=voxels[indices]; # find the values in the images at those positions但这给了
> length(indices)
[1] 20483
> length(voxelsfound)
[1] 20483所以只有第一个时间点的结果。是否有类似的方法来表示这一点,以使它也返回其他时间点(我的想法是voxelsfound=voxels[indices,],但这不起作用),还是只能使用for循环或类似的方法?
在Python中,我会这样做(使用m表示mask,使用v表示voxels),使用nonzero函数立即访问索引:
m = m.reshape(np.prod(m.shape));
v = v.reshape(np.prod(m.shape),v.shape[3]);
v = v[:,np.nonzero(m)].squeeze(); 发布于 2018-07-05 00:19:34
我相信你要找的是:
mask <- which(rowSums(voxels, dims=3) != 0, arr.ind=T)
apply(voxels, 4, `[`, mask)https://stackoverflow.com/questions/51175804
复制相似问题