Solution for using rowfun but with a function handle ?

조회 수: 8 (최근 30일)
Nicolas Douillet
Nicolas Douillet 2024년 8월 27일
편집: Nicolas Douillet 2024년 8월 27일
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

채택된 답변

Steven Lord
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
Nicolas Douillet
Nicolas Douillet 2024년 8월 27일
편집: Nicolas Douillet 2024년 8월 27일
Thank you for this answer.
Ok maybe I misexpressed myself; I am french sorry, please forgive my errors.
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.
For the rest of your questions, it would be quicker for you I think to directly have a look at the code in the link I mentioned.
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 loops are not the best code structure in Matlab to save cpu time)
Steven Lord
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
/MATLAB/toolbox/matlab/datatypes/tabular/@tabular/rowfun.m % tabular method
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
ans = 0
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
Sine of a figure handle threw error: Undefined function 'sin' for input arguments of type 'matlab.ui.Figure'.
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by