1 |
|
---|
2 |
function A2=safeslice(A,dimm,index); |
---|
3 |
% |
---|
4 |
% SAFESLICE.M -- slices any-dimensional matrix A along a given |
---|
5 |
% dimension and a given index. Useful if the |
---|
6 |
% actual dimensions or size of A are unknown |
---|
7 |
% or if the size of A varies. |
---|
8 |
% |
---|
9 |
% B = safeslice(A,dimm,index); |
---|
10 |
% |
---|
11 |
% SAFESLICE requires the following inputs |
---|
12 |
% A - input matrix of any size or shape |
---|
13 |
% dimm - dimension along which to sample |
---|
14 |
% index - index of sampled dimension |
---|
15 |
% |
---|
16 |
% A=reshape(1:60,[3 4 5]); % size(A)=[3 4 5]; |
---|
17 |
% |
---|
18 |
% A2=safeslice(A,1,[1 3]); % equivalent to A([1 3],:,:) |
---|
19 |
% |
---|
20 |
% Calls: util/fmap |
---|
21 |
% |
---|
22 |
% Catherine R. Edwards |
---|
23 |
% Last modified: 10 Sep 2002 |
---|
24 |
% |
---|
25 |
|
---|
26 |
% input checks |
---|
27 |
if(nargin~=3); error('SAFESLICE requires 3 inputs'); end |
---|
28 |
|
---|
29 |
asize=num2cell(-size(A)); |
---|
30 |
|
---|
31 |
if(max(index)>-asize{dimm}) |
---|
32 |
error('Index exceeds matrix dimensions along dimension specified.'); |
---|
33 |
elseif(dimm>length(asize)) |
---|
34 |
error('Dimension specified does not exist in input matrix.'); |
---|
35 |
end |
---|
36 |
|
---|
37 |
cellind=fmap('colon',asize,-1);cellind=fmap('uminus',cellind); |
---|
38 |
cellind=fmap('sort',cellind); cellind{dimm}=index; |
---|
39 |
|
---|
40 |
A2=A(cellind{:}); |
---|
41 |
|
---|
42 |
return; |
---|