Perform function on each vector within an array

9 次查看(过去 30 天)
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

采纳的回答

dpb
dpb 2017-6-7
编辑:dpb 2017-6-7
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...
  2 个评论
Adam Danz
Adam Danz 2017-6-7
编辑:Adam Danz 2017-6-7
Thank you dpb! You're right that it works with cellfun rather than arrayfun. You might want to edit your examples where you used arrayfun.
As for the second part of my question, I want to select portions of each vector within the cell array by using indices. startIdx and stopIdx in my example are the first and final indicies I'd like to select for each vector. So startIdx(2):stopIdx(2) will be used to pull elements out of outcomes{2}.
Thanks for your help.
dpb
dpb 2017-6-7
Ah! arrayfun on the brain, I guess--did make the correction, thanks.
The latter may just be a loop; all the ways otherwise that come to me at the moment are so much more complex as cellfun requires multiple cell inputs be same size whereas you've got the cell content with a given vector but the second is also

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by