Perform function on each vector within an array

조회 수: 10 (최근 30일)
Adam Danz
Adam Danz 2017년 6월 7일
댓글: dpb 2017년 6월 7일
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