简体   繁体   中英

How to evaluate multiple arguments and get multiple outputs with cellfun in MATLAB

Suposse the next two variables:

A={[];[1 2];[3]};
B={[10 20 30];[40 50 60];[70 80 90]};

I need to get C1 and C2, who are:

C1={[];[40 50];[90]}; % Corresponding value B(A), like B{2,1}([1 2])=[40 50];
C2={[];[45];[90]}; % Mean, like for ex: mean(B{2,1}([1 2]))=mean([40 50])=45;

As you can see, I need to make something with a cellfun , but I don't know how to evaluate two or more input arguments, and have two or more outputs.

like:

[C1,C2]=cellfun(@function,A,B)

I'll really appreciate any information, Thanks!

It's easy with arrayfun and cellfun . First compute C1 , and from that compute C2 :

C1 = arrayfun(@(k) B{k}(A{k}), 1:size(A,1), 'UniformOutput', false);
C2 = cellfun(@mean, C1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM