vecfun

版本 1.1.0.0 (1.7 KB) 作者: Adam Gripton
Applies a function to each entry of an N-dimensional array comprised of vectors.
247.0 次下载
更新时间 2012/2/20

查看许可证

M = vecfun (f, A, dim)

vecfun applies a function f with column vector input to an N-dimensional array:

A: size [d1,d2, ... ,dD]

where one of the dimensions of A is comprised of possible vector inputs into f, and f returns an array of P elements.

Therefore, the output of vecfun applied to f on A along dimension dim will be of the same number of dimensions as A and have:

M: size [d1,d2, ... , P , ... , dD]

where d[dim] = P.

Example:

A = randn(2,3,5,7);
f = @(p) [p(1)+p(2);p(2)+p(3); ...
p(3)+p(4);p(4)+p(5)];
% f takes a five element column vector p
% and returns a four element output
M = vecfun(f,A,3);
size(M)

% returns [2,3,4,7]
% f:R5 -> R4 has acted along the third
% dimension of A as required.

*****
See also my function `chooseargs' (ID 35115) which works well in conjunction with vecfun.
*****

Example of combined usage:

A=randn(20,5);
Ai=zeros(20,1);
for k=1:20
[~,Aik]=min(A(k,:));
Ai(k)=Aik;
end

can be replaced with:

Ai=vecfun(@(s) chooseargs(@min,2,[],s),A,2);

*****

引用格式

Adam Gripton (2024). vecfun (https://www.mathworks.com/matlabcentral/fileexchange/35086-vecfun), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2011b
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.1.0.0

Added extra information about combined usage with the `chooseargs' function.

1.0.0.0