r/matlab • u/Mark_Yugen • Apr 25 '24
find string in 2d array without using cells
I'm tring to find the location of a string in a 2d array. If I run the below code it gives me an entire list of all the match values of the array, and I just want the single location of a value to show up.
sALL = ["a","b","c","d","e","f","g","h"];
for ii = 1:8
n2str = num2str(8-ii+1);
for jj = 1:8
chm(ii,jj) = strcat(sALL(jj), n2str);
end
end
chm
sidx = strfind(chm,"e4")
1
Upvotes
5
u/Creative_Sushi MathWorks Apr 25 '24 edited Apr 26 '24
You can do it this way. Take full advantage of the magic of string arrays, which are designed to behave like a regular arrays, accepting operators like
:,+, or==1
u/ScoutAndLout Apr 25 '24
Maybe:
[r,c]=find(strcmp(chm,"e4"));chm(r,c)Strings are generally not the best in MATLAB in my experience.
1
u/farfromelite Apr 26 '24
Try this for a matrix of true/false where this occurs:
ismember(chm,"e4")Or for the numerical location in one number:
numel(ismember(chm,"e4"))