Perform function on each vector within an array

8 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by