Skip to content

Commit

Permalink
add common functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Mar 28, 2017
1 parent 330fe22 commit 88a9ee4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
20 changes: 20 additions & 0 deletions common/slice.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function B = slice(A, dim, index)
% slice(A,2,index) = A(:,index,:)
sz = size(A);
sz(dim) = numel(index);
IDX = cell(1,ndims(A));
for i = 1:ndims(A)
if i == dim
idx = index;
else
idx = 1:sz(i);
end
shape = ones(1,ndims(A));
shape(i) = sz(i);
idx = reshape(idx,shape);
shape = sz;
shape(i) = 1;
idx = repmat(idx,shape);
IDX{i} = idx(:);
end
B = reshape(A(sub2ind(size(A),IDX{:})),sz);
16 changes: 16 additions & 0 deletions common/sub.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function B = sub(A, varargin)
% submat(A,i,j,k) = A(i;j;k)
assert(ndims(A)==numel(varargin));
sz = cellfun(@numel,varargin);
IDX = cell(1,ndims(A));
for i = 1:ndims(A)
idx = varargin{i};
shape = ones(1,ndims(A));
shape(i) = sz(i);
idx = reshape(idx,shape);
shape = sz;
shape(i) = 1;
idx = repmat(idx,shape);
IDX{i} = idx(:);
end
B = reshape(A(sub2ind(size(A),IDX{:})),sz);

0 comments on commit 88a9ee4

Please sign in to comment.