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

1 view (last 30 days)
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 Comments
Cedric
Cedric on 6 Aug 2015
Edited: Cedric on 6 Aug 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

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 6 Aug 2015
Edited: James Tursa on 6 Aug 2015
your_matrix = 1621 x 1 matrix containing values 1 - 4
site = some value 1 - 4
indexes = find( your_matrix == site );
  6 Comments
Elena H.
Elena H. on 6 Aug 2015
Edited: James Tursa on 6 Aug 2015
size(site)= [1 1] to [1 4]
class(site)= double
size(data)= [1621 1]
class(data)= double
James Tursa
James Tursa on 6 Aug 2015
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));

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 6 Aug 2015
[tf, idx] = ismember(site, data);
Now where tf(K) is true, idx(K) gives an index of site(K) in data

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!