MATLAB >> palindrome code

by Raymond » Fri, 17 Sep 2010 02:22:07 GMT

I'm trying to do a standard palindrome (Madam I'm Adam). I know that I have to just toss out the spaces and punctuation, and then test whether it's true or false.
Problem is how do I toss out spaces and punctuation?

a='hello world'
s=double(a)

now how do I toss out spaces and punctuation?

Thanks!

MATLAB >> palindrome code

by Sean » Fri, 17 Sep 2010 02:52:23 GMT



No reason to convert to double. As for removing spaces and punctuation
help isspace
a(isspace(a))=[];

Here's a random way for getting rid of punctuation that just occurred to me. Since lower case letters don't equal upper case letters but lower punctuation does = upper punctuation
a = a(lower(a)~=upper(a))

a='hello world'
isequal(a,fliplr(a))

a='level'
isequal(a,fliplr(a))

MATLAB >> palindrome code

by Walter Roberson » Fri, 17 Sep 2010 03:00:39 GMT


Don't toss out things, keep what is required.

a = a(ismember(a,intersect(a,['a':'z' 'A':'Z'])));

MATLAB >> palindrome code

by Sean » Fri, 17 Sep 2010 03:09:06 GMT


That won't work if you want to keep numbers :(

This would account for numbers:
a = a(lower(a)~=upper(a)|ismember(int8(a),48:57))

MATLAB >> palindrome code

by someone » Fri, 17 Sep 2010 03:13:22 GMT


% I'm not sure isspace will remove ALL punctuation marks.

doc isletter

% might be more useful here.



% Or (another way):

palTestString = 'le; ve l.';
a = palTestString(isletter(palTestString));
strcmp(a,fliplr(a))

MATLAB >> palindrome code

by Walter Roberson » Fri, 17 Sep 2010 03:18:49 GMT


I wonder... how does isletter() work with non-English letters?

Aren't there some alphabets in which the lower-case equivalent of some of the
upper-case characters require 2 characters to write? I thought I once
encountered something like that in German.

MATLAB >> palindrome code

by someone » Fri, 17 Sep 2010 03:21:20 GMT


% Perhaps one more tweak is needed:

palTestString = 'Le; ve l.';
a = lower(palTestString(isletter(palTestString)));
strcmp(a,fliplr(a))

MATLAB >> palindrome code

by Sean » Fri, 17 Sep 2010 03:39:20 GMT


I think Walter's is the best solution since it can be easily adapted to include numbers or specific symbols etc.
So we want all letters, numbers and the pound sign:

a = a(ismember(lower(a),intersect(a,['a':'z' '0':'9' '#'])));

MATLAB >> palindrome code

by Walter Roberson » Fri, 17 Sep 2010 04:38:10 GMT


ssssh!

---
"Oh Walter! You are the master of obfuscation!!"

MATLAB >> palindrome code

by Jos (10584) » Fri, 17 Sep 2010 05:11:20 GMT


German has the lower-case , which turns into a two S's in upper case:
http://en.wikipedia.org/wiki/

MATLAB >> palindrome code

by Walter Roberson » Fri, 17 Sep 2010 05:42:13 GMT


Ah, I was thinking of that letter but I wasn't sure about it.

The implication would seem to be that in at least some cases, two S's in a row
should become a single lower-case character if everything is converted to
lowercase for comparison.

In German, is it possible for two upper-case S's to appear adjacent to each
other (possibly with punctuation or spaces between) that do not represent this
letter? I seem to recall that German capitalizes proper nouns, but are there
cases where a capital letter can occur in the middle of a word or at the end
of a word? In English, capital letters can occur in the middle of a word only
for names derived from other languages (e.g., VanHooke), or for acronyms
(e.g., ATM); or for trademarks (e.g. MicroSoft); or for abbreviations (e.g.,
kHz). Alternately, is it possible in German for two lower-case s's to appear
adjacent to each other in a word and _not_ to be ??

The point of the above questions is of course to look for palindromes that
would break if SS is lowercased as , and to look for palindromes which are
only successful if SS is lowercased as ss, and to look to see whether SS to
is a mechanical translation or has to be guided by some more intelligence.

Several of the algorithms proposed used tolower() _after_ removing the spaces
and punctuations, but if it is possible in german to have a group of letters
(word or acronym) that can end in S followed by space/punctuation followed by
a word that starts with a single S, then in order for the lower-casing to be
accurate it would have to be done _before_ the punctuation was removed, as
otherwise after removal you might end up with an SS pair that was *not* in
lower-casing. Mind you, it would also be interesting to find one in German
that worked exactly the other way, dependant on the two SS's being jammed
together and matched with ...

MATLAB >> palindrome code

by Jan Simon » Fri, 17 Sep 2010 05:58:04 GMT

Dear Walter,


I cannot see the benefit of the INTERSECT here. Simpler:
a = a(ismember(a, ['a':'z', 'A':'Z']));
(or with '0':'9' etc).

Jan

MATLAB >> palindrome code

by Walter Roberson » Fri, 17 Sep 2010 06:28:10 GMT


Similar Threads

1. palindrome code - MATLAB

2. find near palindromes with regexp?

Hi,

I am trying to find palindromes and near palindromes within
text strings using regular expressions. I used a regexp from
the Matlab help to get 

test = 'aaaaCDEFGGFEDCbbbbbJKLMNOaabbONMLKJaaaa';
P = regexp(test,'(.{3,13}).?(??@fliplr($1))','match');

which correctly gives P = 'CDEFGGFEDC'.

I would also like to be able to find the near palindrome
starting with JKL. By near palindrome I mean something like
JKLMNOaabbONMLKJ where the aabb part may be two or more
nonpalindromic characters. Unfortunately, I think my regular
expression Kung Fu is too weak to see how to do that. Anyone
out there know a simple way to do this?

Thanks,
J



3. M code into programming C code or MATLAB programming code - MATLAB

4. how to transform this C code to MATLAB code

Hi,Firends

A C++ code like this:

char evfile[16];
fread(&evfile, sizeof(evfile), 1, fp); 

fp is a file name;
I want to transform it to MATLAB code, what should I do? 

Thanks for you help!

5. matlab codes to visual C# code - MATLAB

6. convert C++ code to M-code

hi,

i want to convert a C++/C code to Matlab code (M-file) and 
viceversa, what instructions do i need??

Do you know some simple example that illustrate this?? or 
some tutorial in the internet or something about this 
topic?? something specific and short if possible

thanks for your help

7. Matlab code to Visual Basic code

8. Matlab code to C-code generation using emlc

Hi,

I am trying to convert my matlab code to C-code using the emlc command (of embedded matlab). Upon execution,I realize the following problems-
1. Many .c and header files are generated, how to compile them is an issue for me...
2. A lot of header files are not found when I try to compile the files using Dev-Cpp
3. No main function is generated resulting in the error- invalid reference to win32@16

Kindly help. Thanks a lot in advance.
-Pradyot