Splitapply within table taking column vector input and returning column vector output
Show older comments
I am having trouble submitting a grouped column vector to a function which returns a column vector using splitapply.
In a separate file, I have defined the following:
function share_remaining = share_remaining(search)
cp = (cumprod(1-search));
share_remaining = [1; cp(1:end-1)];
end
I am trying to run:
test = table([1:1000]');
test.group = repmat(1, 1000, 1); % I use a group value of 1 to test splitapply on the whole dataset, don't think this should cause problems
test.share_remaining = splitapply(@(x){share_remaining(x)}, test.Var1, test.group);
but get the error (after running the splitapply line):
To assign to or create a variable in a table, the number of rows must match the height of the table.
I have checked the dimentions of the input and output for share_remaining and they appear to be the same.
I've found the following in the documentation at https://www.mathworks.com/help/matlab/ref/splitapply.html:
If func returns a nonscalar output argument, then the argument must be oriented so that splitapply can concatenate the output arguments from successive calls to func. For example, if the input data variables are column vectors, then func must return either a scalar or a row vector as an output argument.
I think this describes the problem I'm having, but I don't know of an easy solution. Any suggestions?
Thanks.
Accepted Answer
More Answers (0)
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!