%// Inputs
A = randi(9,3,4,3)
idx = [2 4 3]
%// Get size of input array, A
[M,N,P] = size(A)
%// Permute A to bring the columns as the first dimension
Ap = permute(A,[2 1 3])
%// Per 3D slice offset linear indices
offset = bsxfun(@plus,[0:M-1]'*N,[0:P-1]*M*N) %//'
%// Get 3D array linear indices and remove those from permuted array
Ap(bsxfun(@plus,idx(:),offset)) = []
%// Permute back to get the desired output
out = permute(reshape(Ap,3,3,3),[2 1 3])
可以使用下面的linear indexing来完成 . 注意它是's better to work down columns (because of Matlab' s column-major order),这意味着在开头和结尾处进行转置:
A = [ 1 7 3 4
1 4 4 6
2 7 8 9 ];
v = [2 4 3]; %// the number of elements of v must equal the number of rows of A
B = A.'; %'// transpose to work down columns
[m, n] = size(B);
ind = v + (0:n-1)*m; %// linear index of elements to be removed
B(ind) = []; %// remove those elements. Returns a vector
B = reshape(B, m-1, []).'; %'// reshape that vector into a matrix, and transpose back
2 回答
这是一种使用bsxfun和permute来解决3D数组情况的方法,假设您要在所有3D切片中每行删除索引元素 -
样品运行 -
可以使用下面的linear indexing来完成 . 注意它是's better to work down columns (because of Matlab' s column-major order),这意味着在开头和结尾处进行转置: