get_submatrix.m

来自「小波压缩编码平台软件包」· M 代码 · 共 32 行

M
32
字号
function [S,Sind] = get_submatrix(X,Ssiz,Soff)
%[S,Sind] = get_submatrix(X,Ssiz,Soff)
%Version: 1.00, Date: 2005/01/12, author: Nikola Sprljan
%Extracts submatrix from a multidimensional matrix
%
%Input: 
% X - matrix of n dimensions
% Ssiz - submatrix size to be extracted
% Soff - submatrix starts at this offset
%
%Output:
% S - submatrix
% Sind - indices of the extracted submatrix in the original submatrix
%
%Note:
% To use the extracted indices, use: X(Sind{:})
%
%Example: 
% Suppose I have an N-dimensional matrix X, and I want to extract the 
% submatrix of which each dimension is of half the size than in X (with 
% the offset at the first element of X). Then, it would be called:
% S = get_submatrix(X,ceil(size(X)/2));

if isvector(X) n = 1;
else n = ndims(X);end;
if (nargin < 3) Soff = ones(n,1);end; %sets the default offset
Sind = repmat({':'},n,1);
for i = 1:n
    Sind{i} = Soff(i):(Soff(i) + Ssiz(i) - 1);
end;
S = X(Sind{:});

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?