bsxfunっぽいもの

アルゴリズム
スポンサーリンク
function [ out ] = bsxfun( func, target, data )
%[ out ] = bsxfun( func, target, data )
%MATLABの新しいのにある、bsxfunっぽいもの。
%http://www.mathworks.co.jp/access/helpdesk_ja_JP/help/techdoc/ref/bsxfun.html
%『入力』
%func   種類
%target A
%data   B
%『出力』
%out	出力

name  = func2str(func);
MINUS   = 0;
TIMES   = 1;
RDIVIDE = 2;
LDIVIDE = 3;
PLUS   = 4;

type = -1;
if strcmp(name,'plus'),
	 type = PLUS;
elseif strcmp(name,'minus'),
	 type = MINUS;
elseif strcmp(name,'times'),
	 type = TIMES;
elseif strcmp(name,'rdivide'),
	 type = RDIVIDE;
elseif strcmp(name,'ldivide'),
	 type = LDIVIDE;
end

%row	height
%column width

[row,   column] = size(target);
[height, width] = size(data);

if ((height==1)&&(width==1)),
	 if (type == PLUS),
		  out = target + data;
	 elseif (type == MINUS),
		  out = target – data;
	 elseif (type == TIMES),
		  out = target .* data;
	 elseif (type == RDIVIDE),
		  out = target ./ data;
	 elseif (type == LDIVIDE),
		  out = target .\ data;
	 end
else
	 out = zeros(row,column);
	 for x=1:row,
		  for y=1:column,
			   a = mod(x – 1,height) + 1;
			   b = mod(y – 1,width)  + 1;
		  if (type == PLUS),
			   out(x,y) = target(x,y) + data(a,b);
		  elseif (type == MINUS),
			   out(x,y) = target(x,y) – data(a,b);
		  elseif (type == TIMES),
			   out(x,y) = target(x,y) * data(a,b);
		  elseif (type == RDIVIDE),
			   out(x,y) = target(x,y) / data(a,b);
		  elseif (type == LDIVIDE),
			   out(x,y) = target(x,y) \ data(a,b);
		  end
	 end
end

end

コメント

タイトルとURLをコピーしました