find the index number using bsxfun command

3 vues (au cours des 30 derniers jours)
Abdullah Türk
Abdullah Türk le 29 Mar 2023
Commenté : Abdullah Türk le 30 Mar 2023
Hi,
I have two array as following:
A = [12 13 15 1];
B = [1 2 12 15 10 13 11 14 3 4 5 6 7 8 9 16];
I want to find position of the numbers in array A in array B. To doing this, I can for loop but can we use bsxfun command to accomplish this?

Réponse acceptée

Steven Lord
Steven Lord le 29 Mar 2023
I'd prefer using the ismember function instead of bsxfun.
  1 commentaire
Abdullah Türk
Abdullah Türk le 30 Mar 2023
Thanks Steven, I used ismember function. It is really more pratic.

Connectez-vous pour commenter.

Plus de réponses (3)

Antoni Garcia-Herreros
Antoni Garcia-Herreros le 29 Mar 2023
Hello,
Yes, you can use the bsxfun command.
C = bsxfun(@eq, B, A(:));
[~,id]=ind2sub(size(C),find(C(:)==1)) % id contain the indices where B is equal no an element in A
  1 commentaire
Abdullah Türk
Abdullah Türk le 30 Mar 2023
Thank you Antoni Garcia-Herreros. I tried it and it is useful. Thanks again.

Connectez-vous pour commenter.


John D'Errico
John D'Errico le 29 Mar 2023
Modifié(e) : John D'Errico le 29 Mar 2023
But why would you use bsxfun at all to do this?
A = [12 13 15 1];
B = [1 2 12 15 10 13 11 14 3 4 5 6 7 8 9 16];
Use ismember instead.
help ismember
ISMEMBER True for set member. LIA = ISMEMBER(A,B) for arrays A and B returns an array of the same size as A containing true where the elements of A are in B and false otherwise. LIA = ISMEMBER(A,B,'rows') for matrices A and B with the same number of columns, returns a vector containing true where the rows of A are also rows of B and false otherwise. [LIA,LOCB] = ISMEMBER(A,B) also returns an array LOCB containing the lowest absolute index in B for each element in A which is a member of B and 0 if there is no such index. [LIA,LOCB] = ISMEMBER(A,B,'rows') also returns a vector LOCB containing the lowest absolute index in B for each row in A which is a member of B and 0 if there is no such index. The behavior of ISMEMBER has changed. This includes: - occurrence of indices in LOCB switched from highest to lowest - tighter restrictions on combinations of classes If this change in behavior has adversely affected your code, you may preserve the previous behavior with: [LIA,LOCB] = ISMEMBER(A,B,'legacy') [LIA,LOCB] = ISMEMBER(A,B,'rows','legacy') Examples: a = [9 9 8 8 7 7 7 6 6 6 5 5 4 4 2 1 1 1] b = [1 1 1 3 3 3 3 3 4 4 4 4 4 9 9 9] [lia1,locb1] = ismember(a,b) % returns lia1 = [1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1] locb1 = [14 14 0 0 0 0 0 0 0 0 0 0 9 9 0 1 1 1] [lia,locb] = ismember([1 NaN 2 3],[3 4 NaN 1]) % NaNs compare as not equal, so this returns lia = [1 0 0 1], locb = [4 0 0 1] Class support for inputs A and B, where A and B must be of the same class unless stated otherwise: - logical, char, all numeric classes (may combine with double arrays) - cell arrays of strings (may combine with char arrays) -- 'rows' option is not supported for cell arrays - objects with methods SORT (SORTROWS for the 'rows' option), EQ and NE -- including heterogeneous arrays derived from the same root class See also ISMEMBERTOL, INTERSECT, UNION, UNIQUE, UNIQUETOL, SETDIFF, SETXOR, SORT, SORTROWS. Documentation for ismember doc ismember Other uses of ismember categorical/ismember datetime/ismember sym/ismember cell/ismember duration/ismember tabular/ismember codistributed/ismember gpuArray/ismember tall/ismember dataset/ismember mtree/ismember
[I,J] = ismember(A,B)
I = 1×4 logical array
1 1 1 1
J = 1×4
3 6 4 1
So I tells you if an element s found at all in B. J tells you the FIRST location in B, of each element of A.
If some of the elements in A might appear multiple times in B (I'll change B for that to happen), then you might do this, using rangesearch:
Bhat = [B,13 1]
Bhat = 1×18
1 2 12 15 10 13 11 14 3 4 5 6 7 8 9 16 13 1
IDX = rangesearch(Bhat',A',0)
IDX = 4×1 cell array
{[ 3]} {[6 17]} {[ 4]} {[1 18]}
Which tells you the first element in A (which was 12) appeared at location 3 in Bhat. But 13 appears in two locations, thus 6 and 17.
help rangesearch
RANGESEARCH Radius search. IDX = RANGESEARCH(X,Y,RADIUS) finds all the points in X that are within distance RADIUS for points in Y. Rows of X and Y correspond to observations, and columns correspond to variables. Y must have the same number of columns as X. RADIUS is a numeric non-negative number specifying the radius threshold. IDX is NY-by-1 cell array, where NY is the number of rows in Y. IDX{I} contains the indices of points in X whose distance to Y(I,:) are not greater than RADIUS, and these indices are sorted in the ascending order of the corresponding distance values. [IDX, D] = RANGESEARCH(X,Y,RADIUS) returns a NY-by-1 cell array D. D{I} contains the distance values between Y(I,:) and the corresponding points returned in IDX{I}. [IDX, D]= RANGESEARCH(X,Y,'NAME1',VALUE1,...,'NAMEN',VALUEN) specifies optional argument name/value pairs: Name Value 'NSMethod' Nearest neighbors search method. Value is either: 'kdtree' - Creates and uses a kd-tree to find nearest neighbors. 'kdtree' is only valid when the distance metric is one of the following metrics: - 'euclidean' - 'cityblock' - 'minkowski' - 'chebychev' 'exhaustive' - Uses the exhaustive search algorithm. The distance values from all the points in X to each point in Y are computed to find nearest neighbors. Default is 'kdtree' when the number of columns of X is not greater than 10, X is not sparse, and the distance metric is one of the above 4 metrics; otherwise, default is 'exhaustive'. 'Distance' A string or a function handle specifying the distance metric. The value can be one of the following: 'euclidean' - Euclidean distance (default). 'seuclidean' - Standardized Euclidean distance. Each coordinate difference between X and a query point is scaled by dividing by a scale value S. The default value of S is the standard deviation computed from X, S=NANSTD(X). To specify another value for S, use the 'Scale' argument. 'cityblock' - City Block distance. 'chebychev' - Chebychev distance (maximum coordinate difference). 'minkowski' - Minkowski distance. The default exponent is 2. To specify a different exponent, use the 'P' argument. 'mahalanobis' - Mahalanobis distance, computed using a positive definite covariance matrix C. The default value of C is the sample covariance matrix of X, as computed by NANCOV(X). To specify another value for C, use the 'Cov' argument. 'cosine' - One minus the cosine of the included angle between observations (treated as vectors). 'correlation' - One minus the sample linear correlation between observations (treated as sequences of values). 'spearman' - One minus the sample Spearman's rank correlation between observations (treated as sequences of values). 'hamming' - Hamming distance, percentage of coordinates that differ. 'jaccard' - One minus the Jaccard coefficient, the percentage of nonzero coordinates that differ. function - A distance function specified using @ (for example @DISTFUN). A distance function must be of the form function D2 = DISTFUN(ZI, ZJ), taking as arguments a 1-by-N vector ZI containing a single row of X or Y, an M2-by-N matrix ZJ containing multiple rows of X or Y, and returning an M2-by-1 vector of distances D2, whose Jth element is the distance between the observations ZI and ZJ(J,:). 'P' A positive scalar indicating the exponent of Minkowski distance. This argument is only valid when 'Distance' is 'minkowski'. Default is 2. 'Cov' A positive definite matrix indicating the covariance matrix when computing the Mahalanobis distance. This argument is only valid when 'Distance' is 'mahalanobis'. Default is NANCOV(X). 'Scale' A vector S containing non-negative values, with length equal to the number of columns in X. Each coordinate difference between X and a query point is scaled by the corresponding element of S. This argument is only valid when 'Distance' is 'seuclidean'. Default is NANSTD(X). 'BucketSize' The maximum number of data points in the leaf node of the kd-tree (default is 50). This argument is only meaningful when kd-tree is used for finding nearest neighbors. 'SortIndices' A flag to indicate if output distances and the corresponding indices should be sorted in the order of distances ranging from the smallest to the largest distance. Default is true. Example: % Find the points in X whose distance are not greater than 1.5 to % the points in Y, using the default distance metric 'euclidean'. X = randn(100,5); Y = randn(10, 5); [idx, dist] = rangesearch(X,Y,1.5); See also CREATENS, ExhaustiveSearcher, KDTreeSearcher, KNNSEARCH, PDIST2. Documentation for rangesearch doc rangesearch Other uses of rangesearch ExhaustiveSearcher/rangesearch tall/rangesearch KDTreeSearcher/rangesearch textanalytics/rangesearch NeighborSearcher/rangesearch

Catalytic
Catalytic le 29 Mar 2023
Modifié(e) : Catalytic le 29 Mar 2023
bsxfun has been deprecated since R2016b. Just use implicit expansion.
A = [12 13 15 1];
B = [1 2 12 15 10 13 11 14 3 4 5 6 7 8 9 16];
[~,locations]=max(A'==B,[],2)
locations = 4×1
3 6 4 1
%locations=max( bsxfun(@eq,A',B), [],2) <==Obsolete

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Produits


Version

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by