How can I find each character's index within a matrix, from a vector of characters?
1 次查看(过去 30 天)
显示 更早的评论
I have a matrix of characters
Letters = [A, B, C;
D, E, F;
G, H, I];
Secondarily, I will have a vector of letters, and I need to find the index of each letter in the matrix of Letters. For example,
vec = [A, C, H, I], or it could be any combination of letters. I'm sure there is a simple answer that I am missing. I know normally you need to call the character with a string, but I don't know how to call, say vec(1) as a string (as in, idx = find(Letters == vec(1)), and it would return 1, for A), which I think would solve the problem.
0 个评论
回答(2 个)
Chunru
2023-4-28
Letters =['ABC'; 'DEF'; 'GHI']
vec = 'ACHI'
for i=1:length(vec)
[r, c] = find(Letters == vec(i));
fprintf("%s: (%d, %d)\n", vec(i), r, c)
end
0 个评论
DGM
2023-4-28
If you're talking strictly about character arrays, then:
Letters = ['ABC'; 'DEF'; 'HIJ'];
vec = 'ACHI';
[~, idx] = ismember(vec,Letters) % idx is a linear index into Letters
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!