MATLAB: Perform function on each vector within an array

arrayfunarrayscell arrays

Hello all,
I have a 1xn array, 'outcomes', that stores vectors of varying length.
outcomes =
Columns 1 through 3
[1x63 double] [1x246 double] [1x153 double] ...
First, I'd like to perform ismember() on each vector without using a loop. I tried using arrayfun() but I must be missing something because the following code results in an error. I'd like an output array containing logical vectors that identify membership.
outcomeAccept = [0,8];
arrayfun(@ismember, outcomes, outcomeAccept)
I'd also like to index each vector like this:
startIdx = [10, 20, 30, 40, 50]; %same length as outcomes

stopIdx = [30, 30, 60, 60, 60]; %same length as outcomes
newarray = something(outcomes, startIdx, stopIdx)
Where newarray contains the outcomes vectors trimmed to start-stop indices.
Thank you for any advice, Adam

Best Answer

outcomes =
Columns 1 through 3
[1x63 double] [1x246 double] [1x153 double] ...
"First, I'd like to perform ismember() on each vector without using a loop. I tried using arrayfun() results in error ..."
outcomes is a cell array; arrayfun operates on each element of an array. You're looking for cellfun here...
outcomeAccept = [0,8];
outvec=cellfun(@(x)ismember(x,outcomeAccept), outcomes,'uniformoutput',0);
will return the cell array containing the logical vector for each array, each of which is same length as the cell content. To return only the locations that are found,
outidx=cellfun(@(x)find(ismember(x,outcomeAccept)), outcomes,'uniformoutput',0);
I don't follow the remaining portion of the request, sorry...ok, with the added clarification...
out=cellfun(@(x,i1,i2) x(i1:i2),y,num2cell(startIdx),num2cell(stopIdx),'uniformoutput',0)
NB: The conversion to cell() array of same size as original; this won't work with the original vectors of length 5 when the other array is just three elements long as shown.
>> startIdx = num2cell([10, 20, 30, 40, 50])
startIdx =
[10] [20] [30] [40] [50]
>> stopIdx = num2cell([30, 30, 60, 60, 60]);
>> cellfun(@(x,i1,i2) x(i1:i2),y,startIdx,stopIdx,'uniformoutput',0)
Error using cellfun
All of the input arguments must be of the same size and shape.
Previous inputs had size 3 in dimension 2. Input #3 has size 5
>>
Of course, in that case it's not clear what 4 and 5 are to be operating on, either...