Apply function to two cell arrays element-wise

19 views (last 30 days)
dandan on 2 Feb 2018
Commented: dandan on 5 Feb 2018
I have two cell arrays, xdata and CDF. I want to apply kstest element-wise to each pair of xdata and CDF cells (e.g., xdata{1} and CDF{1}, xdata{2} and CDF{2}, etc.).
Each cell of xdata is nx1 (data sample values), and each cell of CDF ix nx2 (theoretical [x,CDF] values).
I can do this with a for loop:
for i=1:length(xdata)
But I'm wondering if there's a clever way to do it more cleanly with anonymous functions and cellfun? Something along the lines of:
[h,p,ks2stat]=deal(cellfun(@(xc) kstest(xc(:,1),xc(:,2:3)),{[xdata CDF]},'Uniform',0));
Any ideas would be appreciated. Thanks!

Accepted Answer

Guillaume on 2 Feb 2018
You're overcomplicating:
[h, p, k2stat] = cellfun(@kstest, xdata, CDF, 'UniformOutput', false);
dandan on 5 Feb 2018
Oops, sorry, you're right. I was simplifying the problem to explain it here and forgot I was referencing structures in the real version. You're correct, the way you originally wrote it does work if xdata and CDF are cell arrays. Thank you and sorry for the confusion!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!