How to find a string within a matrix and output array index to variable
Show older comments
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
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
Elena H.
on 6 Aug 2015
Accepted Answer
More Answers (1)
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 Matrices and Arrays 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!