Perform function on each vector within an array
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
dpb
el 7 de Jun. de 2017
Editada: dpb
el 7 de Jun. de 2017
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 comentarios
dpb
el 7 de Jun. de 2017
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
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!