MATLAB: returns both arguments from ISMEMBER when used inside SPLITAPPLY

How can I access both arguments ismemberwhen it is used internally splitapply?

slitapplyit returns only scalar values ​​for each group, therefore, to calculate non-scalar values ​​for each group (as was returned by the first argument ismemebr), you need to enclose an anonymous function (in this case ismember) inside curly braces {}to return an array of cells.

But now, when I provide two output arguments splitapply, I get an error message:

Output argument "varargout{2}" (and maybe others) not assigned during call to "@(x,y) {ismember(x,y)}"

ADD 1

I can create another function, say, ismember2cellthat would apply ismemberand turn the outputs into arrays of cells:

function [a, b] = ismember2cell(x,y)
  [a,b] = ismember(x,y);
  a = {a};
  b = {b};
end

, , , .

+4
2

- , splitapply , ( , mat2cell accumarray) cellfun . cellfun (, ismember). :

% Sample data:
A = [1 2 3 4 5];
B = [1 2 1 5 5];
G = [1 1 1 2 2];  % Group index

% Group data into cell arrays:
cellA = accumarray(G(:), A(:), [], @(x) {x(:).'});  % See note below about (:).' syntax
cellB = accumarray(G(:), B(:), [], @(x) {x(:).'});

% Apply function:
[Lia, Locb] = cellfun(@ismember, cellA, cellB, 'UniformOutput', false);

. - , , accumarray ( ). , , . (:).' - , , x, , , .', , .

+3

, :

function varargout = out2cell(varargin)
[x{1:nargout}]=feval(varargin{:});
varargout = num2cell(x);

,

splitapply(@(x,y) out2cell(@ismember, x, y), A, B);
0

Source: https://habr.com/ru/post/1688799/


All Articles