Solution for using rowfun but with a function handle ?
조회 수: 8 (최근 30일)
이전 댓글 표시
Hi,
I want to apply my own function to every row of a numeric matrix. I currently use cellfun but the need for conversion from numeric to cell array and vice versa takes some time, time I want to save.
rowfun doesn't work with function handle, and arrayfun also returns a cell array.
My function has two input and two output arguments.
Is there a Matlab way to do so ?
Edit : I actually want to improve my insiside2Dset function cpu time, and especially line #105 (firt line inside the for loop). Test / example #1 is at the end of comment section above
Thank you for help.
Cheers,
Nicolas
댓글 수: 0
채택된 답변
Steven Lord
2024년 8월 27일
I want to address the points in your question slightly out of order.
rowfun doesn't work with function handle, and arrayfun also returns a cell array.
rowfun absolutely does work with function handles; in fact the first input argument must be a function handle. It doesn't work on numeric arrays; the input A that you pass in as the second input must be a table or timetable array.
I want to apply my own function to every row of a numeric matrix.
My function has two input and two output arguments.
So one of the inputs to your function is a row of the matrix. What's the other? Is it another row of the matrix, is it a constant, is it the result of a previous call to that function on a different row of the matrix, etc.?
Do you want to capture both output arguments for each row? I assume that they are compatibly sized to be assembled into an array? You don't have the scenario where one call could return a 3-element vector and the next a 4-element vector, do you?
The simplest approach is probably going to be a straightforward for loop.
댓글 수: 2
Steven Lord
2024년 8월 27일
Still I get the error message 'Undefined function 'rowfun' for input arguments of type 'function_handle' when I try to use rowfun with a function handle.
Yes, that's because you're calling rowfun and none of the inputs is a table or timetable.
which -all rowfun
It is only defined if at least one of the inputs is a tabular array (a table or timetable.) If you call it with a function handle and a numeric array, MATLAB will look for a rowfun method for the function_handle data type. Since it can't find it, it throws the error you cited. It's the same as if you called the sin function with a figure handle as input. The sin function is defined in MATLAB
sin(0) % works for numbers
but it is not defined for figure handles.
try
sin(figure)
disp("Sine of a figure handle worked")
catch ME
fprintf("Sine of a figure handle threw error:\n%s\n", ME.message)
end
I already tried a classical for loop of course, but this is not optimal since there already is a global for loop (and nested for loop are not the best code structure in Matlab to save cpu time)
sigh "for loops are slow in MATLAB" is something that people have been saying for decades and it's at best an oversimplification at this point. We have made a lot of effort to improve the performance of loops (and the rest of MATLAB) in that time.
What was the timing data that you gathered to compare your current cellfun approach and the for loop approach for specific data sets? How fast are you hoping / expecting your code to run for those problems? And how does this compare to the performance of similar functions in MATLAB (like the inpolygon function cited as a See Also in your function or the isinterior function for polyshape objects?)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!