⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expand.m

📁 实现地震勘探中
💻 M
字号:
function y=expand(a,b)
%EXPAND Expand Singleton Dimensions.
% EXPAND(A,B) expands A by replication to match the size of B so that
% element by element mathematical and logical operations between A and B
% are defined.
%
% All nonsingleton dimensions of A must match those of B.
% All singleton dimensions of A are replicated to match those of B.
%
% Examples:
% EXPAND( [1 2], EYE(2) ) produces [1 2;1 2]
% EXPAND( [1;2], EYE(2) ) produces [1 1;2 2]
% EXPAND( [1 2], CAT(3,EYE(2),ONES(2)) ) expands [1 2] in two dimensions
% ans(:,:,1) =
%     1     2
%     1     2
% ans(:,:,2) =
%     1     2
%     1     2
%
% See also REPMAT, CAT

% D.C. Hanselman, University of Maine, Orono, ME 04469
% MasteringMatlab@yahoo.com
% Mastering MATLAB 7
% 2006-02-16

asiz=[size(a) ones(1,ndims(b)-ndims(a))]; % try to make A look as big as B
bsiz=size(b);

if length(asiz)>length(bsiz)
   error('A Must Have The Same Number of Dimensions as B.')
end

ns=(asiz>1);              % nonsingleton dims of A
sd=(~ns);                 % singleton dims of A
if all(asiz==bsiz)        % no work to do, A is already as big as B
   y=a;
   
elseif all(ns==0)         % A is a scalar (protect next test from emptys)
   y=repmat(a,bsiz);      % expand scalar A to the size of B
   
elseif any(asiz(ns)~=bsiz(ns))
   error('All NonSingleton Dimensions of A Must Match Those of B.')
   
else                      % finally, do it
   rep=ones(size(bsiz));  % start with single replication of all dimensions
   rep(sd)=bsiz(sd);      % poke in replications of A required to match B
   y=repmat(a,rep);       % let repmat do the nitty-gritty work
end

⌨️ 快捷键说明

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