CodexBloom - Programming Q&A Platform

MATLAB: guide with combining cell arrays of varying lengths using 'cellfun'

πŸ‘€ Views: 19 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-16
matlab cell-arrays cellfun MATLAB

I'm attempting to set up I've been working on this all day and I'm trying to combine multiple cell arrays of varying lengths into a single cell array while applying a function to each element... However, I'm working with unexpected results when utilizing `cellfun`. I expected to get a uniform output, but it seems to truncate or misalign data from the input arrays. Here’s a simplified example of what I’m trying to achieve: ```matlab A = {1, 2, 3}; B = {4, 5}; C = {6, 7, 8, 9}; cellArray = {A, B, C}; result = cellfun(@(x) sum(cell2mat(x)), cellArray, 'UniformOutput', false); ``` I expected `result` to contain the sums of each cell array: {6, 9, 30}. However, it seems to only return the sum of the first input, and I get the following behavior message: ``` behavior using cellfun All cell arrays must have the same size. ``` I’ve tried using `cell2mat` directly within the function, but that results in dimension mismatch errors when the lengths vary. Is there a way to handle cell arrays of different lengths in `cellfun`, or do I need to implement a loop instead? Any insights would be appreciated! This issue appeared after updating to Matlab 3.10. Thanks for any help you can provide! The stack includes Matlab and several other technologies. This is my first time working with Matlab 3.11. I'd be grateful for any help. Could someone point me to the right documentation?