How can I multiply each element of a cell array, defined as an anonymous function handle?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
I have a cell array FS which contains a function handle at each cell. I want to multiply the output of each cell with each other (giving each function handle the SAME input). The function handles in each cell are identical except for the i value stored in them.
Define array as:
N = 5;
for i=1:N
FS{i} = @(x) x+i;
end
i can obtain my desired result by a simple loop:
x = 3;
P = FS{1}(x)
for i=2:length(FS)
P = P * FS{i}(x)
end
However, I wish to define this operation as a new function handle, performing the same action:
b = @(x) FS{1}(x) * FS{2}(x) * FS{3}(x) ... * FS{N}(x)
but this should of course be flexible for any number of elements (FS).
채택된 답변
b = @(x) prod(cellfun(@(F) F(x), FS))
This assumes that the output of each handle is scalar even if x is non-scalar.
If you really do mean * as in algebraic matrix multiplication, which assumes that the results of each function handle will be either a scalar or a matrix whose number of columns is the same as the number of rows of output of the next function handle (e.g., square matrices work well) then
b = @(x) fold(@mtimes, cellfun(@(F) F(x), FS), 1);
댓글 수: 9
it does need to be an algebraic matrix multiplication, since my result is not necessarily scalar. I have another more complex configuration where the output of each function handle is actually an object of an abstract class. So it is important that the format would correspond to:
b = @(x) FS{1}(x) * FS{2}(x) * FS{3}(x) ... * FS{N}(x)
Is there a way to avoid using fold? I would prefer to avoid using the symbolic toolbox. But thanks a lot !
This use of fold comes down to
F = @mtimes;
result = v{1};
for j = 2:numel(v)
result = F(result, v{j});
end
In other words, just program your own.
With non-scalar outputs you will need to add 'uniform', 0 to the cellfun() call.
*because I don't have access to this toolbox
Probably the best thing to do would be to write your own routine that accepted a cell array of function handles and an input array, and implemented the matrix product in a loop.
Using anonymous functions is not always the most efficient route. Indeed, it is seldom the most efficient route: tests show that anonymous functions are distinctly slower than regular functions.
I see. I have tried to figure another way, but the way my script is structured, it seems like the most flexible way. In this case I am trading flexibility over efficiency. Thanks for the reply
You can still use
b = @(x) MultFuns(FS, x)
assuming you put the work code into function MultFuns .
And other b might be anonymous functions that did the work more in-line.
There is no shame in writing a helper function to implement functionality that Mathworks does not provide in your available toolboxes.
Thanks. I ended up making a function like that, defined as:
function cascadedOut = cascadeCells(fcellArray,F)
cascadedOut = fcellArray{1}(F);
for i=2:length(fcellArray)
cascadedOut = cascadedOut * fcellArray{i}(F);
end
end
This worked!
Looks good, and is probably faster than the alternatives.
Hi, interesting approach.
Could you please share the final working version of the code?
I tried to replicate it from by end, but something doesn't seem quite right.
Thanks
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
