How do I find a value in a vector and then create a new vector with those value?

I am having an issue with the following task. How do I create a vector that shows the position of the alphabetic characters as numerical values? Create a vector Alphas that has the numeric positions of all alphabetic characters in TS1. For Example: TS1='%@3Gb6' returns Alphas=[4 5]. I am using the Matlab R2015b software.

 採用された回答

dpb
dpb 2016 年 1 月 20 日
編集済み: dpb 2016 年 1 月 21 日
>> TS1='%@3Gb6';
>> find(iswithin(TS1,'A','z'))
ans =
4 5
>>
>> type iswithin
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
% Utility routine courtesy dpbozarth (aka dpb)
flg= (x>=lo) & (x<=hi);
>>
ERRATUM
As noted, forgot there were some characters between the lower- and upper-case ones that will screw up count/position if not accounted for.
find(iswithin(TS1,'A','z') & isletter(TS1))
does work albeit now it may be as simple as to write the regular expression expression.
And, DOH!
find(isletter(TS1))
actually is all that's needed.

1 件のコメント

dpb
dpb 2016 年 1 月 20 日
Actually, the above isn't exact as there are a few characters between 'z' and 'A'. Just so happens none of them is in the subject string. would have to

サインインしてコメントする。

その他の回答 (1 件)

Star Strider
Star Strider 2016 年 1 月 20 日
This is one possibility:
TS1='%@3Gb6';
[AlphaSt,AlphaEnd] = regexp(TS1, '[A-Za-z]+')
AlphaSt =
4
AlphaEnd =
5

2 件のコメント

Actually, the simpler regex
pos = regexp(TS1, '[A-Za-z]');
would work better for the OP purpose.
Star Strider
Star Strider 2016 年 1 月 20 日
Thank you, Guillaume. I’m still learning the best ways to use regexp, and with your demonstrated expertise on this and several other topics, I value your Comments.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2016 年 1 月 20 日

編集済み:

dpb
2016 年 1 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by