【发布时间】:2018-07-04 14:16:53
【问题描述】:
我的 R 脚本加载一个 4 维数据集,它是 3D 医学图像的时间序列。我使用时间序列创建一个掩码,用于排除每个时间点值为 0 的体素(3 维像素):
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();
【问题讨论】:
标签: arrays r multidimensional-array indexing nifti