Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

how to find the position of a given number

if the input is 1050, whats the code to get the position of 5.....(answer is third)

0 Comments

Tags

Products

3 Answers

Answer by Andrei Bobrov

Andrei Bobrov (view profile)

on 5 Sep 2012
k = 1050
find(num2str(k)-'0' == 5)

0 Comments

Andrei Bobrov

Andrei Bobrov (view profile)

Answer by José-Luis

José-Luis (view profile)

on 5 Sep 2012
Edited by José-Luis

José-Luis (view profile)

on 6 Sep 2012
your_answer = strfind(num2str(1050),num2str(5));

0 Comments

José-Luis

José-Luis (view profile)

Answer by Image Analyst

Image Analyst (view profile)

on 5 Sep 2012
locationOf5 = strfind('1050', '5')

You can adapt it to other numbers, or pass in string variables instead of hard-coding it if you want.

3 Comments

Thanks Image Analyst !! What would be the code if the input is not a string?

Andrei Bobrov

Andrei Bobrov (view profile)

on 6 Sep 2012

see answer by Jose-Luis

Image Analyst

Image Analyst (view profile)

on 6 Sep 2012
locationOfDigit = strfind(num2str(yourFullNumber), num2str(yourDesiredDigit))
Image Analyst

Image Analyst (view profile)

Contact us