How to find a string within a matrix and output array index to variable

First off, I hope that was titled right.
Secondly, here is my dilemma:
I have this matrix that is [1621x1] with 1-4 numbers in each row. I have a variable, called 'site' that has one of these numbers as its value.
I want to match the variable 'site',containing a number, to a number in a [1621x1] matrix and then find out what index the matching number from the [1612x1] matrix has.
I'm unclear about how to search the matrix for the specific value of 'site' and then return wherever site was found in the matrix as an index in a new variable.
I hope that I explained this right. If you need clarification, please ask!!!
Thanks!

2 commentaires

Cedric
Cedric le 6 Août 2015
Modifié(e) : Cedric le 6 Août 2015
Reminds me that I had this for your previous question about sorting/copying files ;-)
outputDir = 'Extracted' ;
dirContent = dir( '1p*' ) ;
flags = cellfun( @(s) s(25)=='r' && (s(26)=='1' || s(26)=='2'), {dirContent.name} ) ;
filenames = {dirContent(flags).name} ;
nFiles = numel( filenames ) ;
for fId = 1 : nFiles
fprintf( 'Copying file %d/%d: %s ..\n', fId, nFiles, filenames{fId} ) ;
copyfile( filenames{fId}, fullfile( outputDir, filenames{fId} )) ;
end

Connectez-vous pour commenter.

 Réponse acceptée

James Tursa
James Tursa le 6 Août 2015
Modifié(e) : James Tursa le 6 Août 2015
your_matrix = 1621 x 1 matrix containing values 1 - 4
site = some value 1 - 4
indexes = find( your_matrix == site );

6 commentaires

This gives me an error saying
Error using ==
Matrix dimensions must agree.
Error in getsol (line 28)
indexes = find( data == site );Error using ==
Matrix dimensions must agree.
Error in getsol (line 28)
indexes = find( data == site );
I understood that site was a scalar variable containing a value from 1 - 4. From your error this does not appear to be the case. Please state the exact sizes and classes of your variables.
site has 1-4 numbers as part of its value. It's actual value ranges from around 40-2700
You haven't answered my question. I asked for the sizes and classes of the variables involved. E.g., what does this show:
size(site)
class(site)
size(mymatrix)
class(mymatrix)
size(site)= [1 1] to [1 4]
class(site)= double
size(data)= [1621 1]
class(data)= double
Dos this do what you want? It finds the indexes in data where the value matches any of the values present in the variable site.
indexes = find(any(bsxfun(@eq,data,site),2));

Connectez-vous pour commenter.

Plus de réponses (1)

[tf, idx] = ismember(site, data);
Now where tf(K) is true, idx(K) gives an index of site(K) in data

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by