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 comentarios

Cedric
Cedric el 6 de Ag. de 2015
Editada: Cedric el 6 de Ag. de 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

Iniciar sesión para comentar.

 Respuesta aceptada

James Tursa
James Tursa el 6 de Ag. de 2015
Editada: James Tursa el 6 de Ag. de 2015
your_matrix = 1621 x 1 matrix containing values 1 - 4
site = some value 1 - 4
indexes = find( your_matrix == site );

6 comentarios

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));

Iniciar sesión para comentar.

Más respuestas (1)

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

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 6 de Ag. de 2015

Respondida:

el 6 de Ag. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by