How to get row indices of names ends with specified common substring

조회 수: 1 (최근 30일)
Mekala balaji
Mekala balaji 2018년 9월 3일
편집: Paolo 2018년 9월 3일
Hi,
I have below cell array, and want to get row indices of names ends with "_A". I am working in Matlab2016a.
Names:
{'HATY_A';'VANU_BA';'IKLA_A';'PAIK_A';'HANYT_BH';'HAYIO_HNA';'HAKIO_A'}
desired_output:
rowIdx=
1
3
4
7
I use below code: pattern='_A'
rowIdx=endsWith(Names,pattern)
also tried
rowIdx=contains(Names,pattern)
but both doesn't work in 2016a. Kindly someone help how to get my desired output.
  댓글 수: 1
Stephen23
Stephen23 2018년 9월 3일
편집: Stephen23 2018년 9월 3일
For older versions:
>> Names = {'HATY_A';'VANU_BA';'IKLA_A';'PAIK_A';'HANYT_BH';'HAYIO_HNA';'HAKIO_A'};
>> find(~cellfun('isempty',regexp(Names,'_A$','once')))
ans =
1
3
4
7
Note that in many cases using logical indexing is more efficient than subscript indexing.

댓글을 달려면 로그인하십시오.

채택된 답변

Paolo
Paolo 2018년 9월 3일
편집: Paolo 2018년 9월 3일
rowIdx=find(endsWith(names,pattern))
For previous versions:
rowIdx=find(cellfun(@(x) strcmp(x(end-1:end),'_A'),names))

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by