Vectorization of anonymous functions
이전 댓글 표시
Hi everybody,
I am trying to get vectorized outputs from anonymous functions. With a single input, the function returns a vector. I am getting in trouble when all or some part of the output is not function of the input therefore constant. In this case Matlab does not manage to generate a vectorized version of the results because the variable is just not there. I m generating the function with matlabFunction(). A small example of the problem (I don't know how the output relates to the input in advance):
>> f=@(x)[x;0]
f =
@(x)[x;0]
>> f(1)
ans =
1 % everything is fine
0
>> f(1:10)
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in @(x)[x;0]
The output that I would expect would be:
1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0
I wish to add that I cannot use logic statements when later I need to call this function.
Thank you for your time.
Best, Davide
EDIT: This function would solve the problem, but it loses the efficiency I hoped to achieve with vectorization.
function [ out ] = f_vectinput( f,u )
%F_VECTINPUT
dim=size(u,2);
out=zeros(size(f(u(1)),1),dim);
%for column input and column results
for i=1:dim
out(:,i)=f(u(i));
end
end
댓글 수: 5
The error you are getting has nothing to do with anonymous functions, so why put this in your title? This misleads people about what the real topic is.
Nor does it have anything to with "because the variable is just not there": in both of your examples you call your function with one input variable (a scalar and a vector respectively). Perhaps you are confused about the difference between variables and the array elements, in which case you should do the introductory tutorials:
Actually your vector x is one variable, it does not matter how many elements it has (one or one million). These are the standard MATLAB terms: when we learn and use the correct terms it makes communicating on this forum much easier.
Davide Calzolari
2016년 5월 20일
Aha, so you are using matlabFunction. Is there any particular reason why you do this? Is it not possible to write the anonymous function by hand? Why do you need to use matlabFunction ?
If I understand correctly you are trying to get matlabFunction to generate an anonymous function that uses code vectorization? According to the documentation there is certainly an option for specifying that an input is a vector (see the 'Vars' option), but this seems to only permit a known size of this vector.
Davide Calzolari
2016년 5월 20일
Davide Calzolari
2016년 5월 20일
편집: Davide Calzolari
2016년 5월 20일
채택된 답변
추가 답변 (3개)
Matt J
2016년 5월 20일
Not sure what you want the output to be. Two possibilities are
f=@(x)[x(:);0]
and
f=@(x) [x;zeros(size(x))]
댓글 수: 4
Davide Calzolari
2016년 5월 20일
@Davide Calzolari: "I give multiple inputs" is impossible, because the function your showed us only has one input argument: x. Count it: one input, x. The (one) variable x might have many elements, but this has nothing to do with the number of input variables your function has (one). You might like to repeat the MATLAB introductory course to learn these very important basic concepts about MATLAB:
And Matt J's answer does give multiple columns, with a row-vector input:
>> f=@(x) [x;zeros(size(x))];
>> f(1:4)
ans =
1 2 3 4
0 0 0 0
Four columns. Count them.
Davide Calzolari
2016년 5월 20일
편집: Davide Calzolari
2016년 5월 20일
Yue
2023년 8월 31일
Thank you very much, the second one is good.
This function would solve the problem, but it loses the efficiency I hoped to achieve with vectorization.
The efficiency you seek needs to be built directly into f. If you are given f already, there is no general way to further vectorize out(:,i)=f(u(i)) externally. You can eliminate the for-loop in favor of more condensed syntax, e.g.,
out= cell2mat( arrayfun(@(i) f(u(i)), 1:10, 'uni',0) );
but there is no execution efficiency that this brings.
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!