Reputation: 3568
I am trying to provide two parameters to the fun
argument of nlfilter
function. I would like to do this by using a handle to the function assign_value
I created. This is my function:
function y = assign_value(x, ii)
index= x([1 2 3 4 6 7 8 9]);
if ismember(ii, index)==1
x(5)= ii; % 'ii' is the cloud object ID
end
y=x;
end
I already red some MATLAB documentation (e.g. 1, 2, 3), and saw some answers (4, 5, etc.), but I still would need a help to solve my specific problem in order to understand how handles to functions work.
Here is what I'm trying to do (x
is a 9by9 double-class matrix)
ii= 127
y= nlfilter(x, [3 3], @assign_value)
The error I get is:
??? Subscripted assignment dimension mismatch.
Error in ==> nlfilter at 75
b(i,j) = feval(fun,x,params{:});
Any help would be really appreciated, thanks in advance.
ANSWER
Thanks to Acorbe comments,I finally make it. As my y
output of assign_value
function was an array, and the fun
parameter of nlfilter
has to output only scalars, I changed my function to:
function y = assign_value(x, ii)
index= x([1 2 3 4 6 7 8 9]);
if ismember(ii, index)==1
x(5)= ii; % 'ii' is the cloud object ID
end
y=x(5);
end
And doing:
y= nlfilter(x, [3 3], @(x) assign_value(x, ii));
my result is fine. Thanks again to Acorbe for his precious contribution.
Upvotes: 1
Views: 953
Reputation: 8391
If I understand your question correctly you want to produce a one-variable version of your function assign_value
parametrized by the value of ii
. This function will operate the local filtering procedure when called by nlfilter
.
Function handles as you were saying can help; specifically you can define
my_ii = 127;
assign_value_parametric = @(x) assign_value(x,my_ii);
and use it as
y= nlfilter(x, [3 3], assign_value_parametric).
The lambda function assign_value_parametric
depends upon one single dependent variable (x)
since the parameter ii
has been fixed once and for all.
In general, consider that this allows a remarkable series of mechanisms.
In particular when you return the function handle outside the function where it has been defined, the parameters it depends upon are automatically shadowed, nonetheless, they are implicitly used when the function handle is called.
EDIT: further comments on your filtering kernel and on the error you get.
I am afraid the kernel you designed does not produce the behavior you need. Consider that a kernel should return a scalar value which is the filter output at point x
of a given filtering window around x
. In your case you are always returning the original filtering window, possibly with a value changed.
Upvotes: 1