Find the indices of numbers that occur first

2 visualizaciones (últimos 30 días)
Amir Mahmoudi
Amir Mahmoudi el 7 de Oct. de 2024
Comentada: DGM el 7 de Oct. de 2024
X is an array. Find(X, n, 1) gives the index of the first "n" that occurs. Now what of instead of "n" I have a vector like V = [n l m o ...]? The elements of V are unique.
  1 comentario
Steven Lord
Steven Lord el 7 de Oct. de 2024
Find(X, n, 1) gives the index of the first "n" that occurs.
No, it doesn't.
help find
find - Find indices and values of nonzero elements This MATLAB function returns a vector containing the linear indices of each nonzero element in array X. Syntax k = find(X) k = find(X,n) k = find(X,n,direction) [row,col] = find(___) [row,col,v] = find(___) Input Arguments X - Input array scalar | vector | matrix | multidimensional array n - Number of nonzeros to find positive integer scalar direction - Search direction 'first' (default) | 'last' Output Arguments k - Indices to nonzero elements vector row - Row subscripts vector col - Column subscripts vector v - Nonzero elements of X vector Examples openExample('matlab/ZeroAndNonzeroElementsInMatrixExample') openExample('matlab/ElementsSatisfyingAConditionExample') openExample('matlab/ElementsEqualToSpecificValuesExample') openExample('matlab/LastSeveralNonzeroElementsExample') openExample('matlab/ElementsSatisfyingMultipleConditionsExample') openExample('matlab/SubscriptsAndValuesForNonzeroElementsExample') openExample('matlab/SubscriptsOfMultidimensionalArrayExample') See also ind2sub, nonzeros, strfind, sub2ind, Short-Circuit AND, Short-Circuit OR, ismember Introduced in MATLAB before R2006a Documentation for find doc find Other uses of find cgddnode/find cgprojconnections/find coder.asap2.getEcuDescriptions/find coder.dictionary.Section/find coder.mapping.api.CodeMapping/find coder.mapping.api.CodeMappingCPP/find codistributed/find coninputfactor/find database.mongo.connection/find gpuArray/find linearize.advisor.LinearizationAdvisor/find mlreportgen.finder.AxesFinder/find mlreportgen.ppt.Presentation/find mlreportgen.ppt.Slide/find sdo.SimulationTest/find Simulink.data.dictionary.Entry/find Simulink.data.dictionary.Section/find Simulink.ScenarioSimulation/find Simulink.SimulationData.Dataset/find Simulink.SimulationOutput/find slreportgen.finder.DataDictionaryFinder/find slreportgen.finder.EnumerationType/find slreportgen.finder.ModelVariableFinder/find slreportgen.finder.SignalFinder/find slreq.Justification/find slreq.LinkSet/find slreq.modeling/find slreq.Reference/find slreq.ReqSet/find slreq.Requirement/find slreq/find sltest.harness/find sltest.testmanager/find sltest.testsequence/find Stateflow.Chart/find sweepset/find sweepsetfilter/find tall/find
find(X, n) would find the first n non-zero elements in X. 1 is not a valid value for the direction input argument.
Now if n is an integer or something that you know for a fact is in X you could use find(X == n, 1). If you're using release R2024b and have an n that is in or "close to" a value in X you could use find(isapprox(X, n), 1).
x = 0:0.1:1;
y = x(randi(numel(x), 100, 1));
loc = find(isapprox(y, 0.3), 5) % find the first 5 values in y that are approximately 0.3
loc = 1×5
16 18 40 43 48
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y(loc)
ans = 1×5
0.3000 0.3000 0.3000 0.3000 0.3000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Iniciar sesión para comentar.

Respuestas (3)

Walter Roberson
Walter Roberson el 7 de Oct. de 2024
[found, idxV] = ismember(X, V);
This will return the idx of elements of X within V; found(K) will be false if X(K) does not match any elements of V, and otherwise idxV(K) will be the index within V for X(K)
Closely related to this is
[found, idxX] = ismember(V, X);
which returns the index within X of the elements of V
  1 comentario
DGM
DGM el 7 de Oct. de 2024
e.g.
% inputs
v = [0 1 2];
x = randi([0 3],1,10)
x = 1×10
3 3 0 3 3 2 2 3 3 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[vinx,idx] = ismember(v,x)
vinx = 1x3 logical array
1 0 1
idx = 1×3
3 0 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Iniciar sesión para comentar.


Matt J
Matt J el 7 de Oct. de 2024
Editada: Matt J el 7 de Oct. de 2024
You won't be able to do better than a for-loop over the V(i).
  3 comentarios
Matt J
Matt J el 7 de Oct. de 2024
Editada: Matt J el 7 de Oct. de 2024
You can do it in a single line, either with the code below or with your own wrapper function, but it won't execute faster than a for-loop:
out = arrayfun(@(n) find(X,n,1), V,'uni',0)
Steven Lord
Steven Lord el 7 de Oct. de 2024
Actually, I wanted to avoid a for loop.
Why? [If the answer is that you've been told "for loops are slow in MATLAB", tell whoever told you that that it is not really true anymore.]

Iniciar sesión para comentar.


Star Strider
Star Strider el 7 de Oct. de 2024
That is not an appropriate find call. The syntax is incorrect.
X = randperm(9) % Unique Random Vector
X = 1×9
1 6 9 4 3 7 2 8 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
idx = find(X, 2, 1)
Error using find
Option must be of char type.
What do you want to do?
.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by