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.

 Accepted Answer

>> 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 Comment

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

Sign in to comment.

More Answers (1)

This is one possibility:
TS1='%@3Gb6';
[AlphaSt,AlphaEnd] = regexp(TS1, '[A-Za-z]+')
AlphaSt =
4
AlphaEnd =
5

2 Comments

Actually, the simpler regex
pos = regexp(TS1, '[A-Za-z]');
would work better for the OP purpose.
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.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 20 Jan 2016

Edited:

dpb
on 21 Jan 2016

Community Treasure Hunt

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

Start Hunting!