Splitapply within table taking column vector input and returning column vector output

3 ビュー (過去 30 日間)
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.

採用された回答

Adam Danz
Adam Danz 2021 年 4 月 8 日
編集済み: Adam Danz 2021 年 4 月 8 日
Sharing the entire error message is most helpful because it typically tells us what line is causing the error.
Based on the content of the error message, I believe the error is happening when you create the test.group variable. Specifically, the number of rows of group is not equal to the number of rows in your table.
If you want to assign all 1s to that variable, this is much more robust:
test.group = ones(height(test),1);
Update
The second possibility is that the output to the splitapply function is not a column vector with the same height as your table. This is the error in the demo you provided.
temp = splitapply(@(x){share_remaining(x)}, test.Var1, test.group);
>> class(temp)
ans =
'cell'
>> size(temp)
ans =
1 1
In fact, temp is a 1x1 cell array containing a 1000x1 vector.
  6 件のコメント
Isaac Liu
Isaac Liu 2021 年 4 月 8 日
Thanks to your suggestion, I have resolved the bug by running
test.share_remaining = cell2mat(splitapply(@(x){share_remaining(x)}, test.Var1, test.group));
Adam Danz
Adam Danz 2021 年 4 月 8 日
> I was copy-pasting the commands into the command window, hence no line number.
Ahh, that makes sense now. The command line is good for making quick changes and trying out stuff during development but whenever you run into an error I suggest clearing the workspace and run the function/script in its entirety to rull out any problems caused by temporary variable and to get the uistack info in the error message - especially when sharing the error in the forum.
> I have resolved the bug
Looks good.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by