leftimage for poohprod.ru

Palindromes in Matlab - working with indices

Palindromes are phrases, words, numbers or other sequences of characters that can be read the same way in either direction.

In programming, they are used to learn to work with indices in a vector or an ascii string, so... there are many students with homeworks like this:

How do I create this in Matlab? I need to create a program that reads 5 digit integers and detects if it's a palindrome or not.

We can approach the problem in a number of ways, but let’s keep this solution simple. Since we are receiving the digits one-by-one, I propose to use a string input. Then, we utilize the buil-in function ‘num2str’ to transform the iteration number and include or concatenate that index in the string to be displayed to the user.

palindrome in Latin

To find an answer, I propose this naive code.


% I use these instructions to reset screen and memory
clear, clc

% We iterate five times
for i = 1 : 5
% Let's ask the user to enter digits one-by-one
str = ['Give me digit number ' num2str(i) ': '];
s(i) = input(str,
's');
% and we reverse the digits, starting by the end
p(6 - i) = s(i);
end

% Compare the entered string with the reversed one
if s == p
% and answer accordingly
disp ('WOW! Those digits happen to be a palindrome!')
else
disp ('They are NOT a palindrome, sorry...')
end

On the other hand, there’s a function named ‘fliplr’ that exactly flips the array or matrix in left/right direction. We could have this slightly simplified code instead:


% We loop five times

for i = 1 : 5
% Let's input the digits
s(i) = input(['Give me digit number ' num2str(i) ': '],'s');
end

% Compare the entered string with the reversed one
if s == fliplr(s)
% and answer accordingly
disp ('Way to go! Those digits are a palindrome!')
else
disp ('Nope, unfortunately they’re not, sorry...')
end


From 'Palindromes ' to home

From 'Palindromes ' to Matlab Examples

Top

Long strings

Video: concatenation




footer for matlab page



































Related pages


graphing matlabboolean circuitsconverting from binary to hexadecimalpascal's triangle solverregression analysis matlabrect function matlabhow to plot unit step function in matlabmatrix asciimatlab programs for beginnerscompound interest spreadsheetmaking a piecewise function continuousrecursive algorithm for factorialformulas for simple interest and compound interestsemilogy matlabboolean logic xordecimals in binaryamatorization tableprogram calculator quadratic formulahow to convert octal to decimalbmi calculatinghow to plot functions in matlabhow to convert cartesian to polarfibonacci calculator formulamatlab solve linear equationsmatlab invertbinary code letter chartascii to integercalculate fibonaccibisection method algorithmintersection of 2 vectorsfinding factoriallagrange interpolation example problemscholesky decomposition matlabcapacitor discharge graphpoly root findermatlab tutorial for engineerskcl circuit analysismatlab graphical user interfacebmi calculatingpascal triangle logicdecimal to octal examplesascii value chart3d plots in matlablogarithmic regression calculator onlinequadratic formula sheetascii character mapgraphing piecewise functions examplesbinary grey codecapacitor charging curveconvert octal to binarysolve matrix equation matlabeuclidean algorithmsleast squares polynomial fitbernoulli trial calculatormatlab shadecombinational logic circuits examplespoly matlabdefine salvage valuedraw rectangle on image matlabcode to convert decimal to binaryhow to plot a polynomial function in matlabmatlab nonlinear regressionconverting from polar to cartesiannodal analysis calculatorsawtooth fourier transformfor loops in matlabdomain and range of piecewise functionspentagon area codebisection method matlab code examplesolve 3 simultaneous equations onlineconverting cartesian to polartrendline matlabascii values for lettersrc circuit differential equation derivationalgorithm to find factorial of a number using recursiontower of hanoi logic