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

Thread Subject:
Comparing individual characters of strings

Subject: Comparing individual characters of strings

From: Nate

Date: 5 Oct, 2011 22:22:07

Message: 1 of 2

I am trying to compare 2 strings and see if any characters in the first string are in the second string

Here is an example,
s1 = 'as';
s2 = 'asd';

result = zeros(length(s1),length(s2));
for i = 1:length(s1)
    result(i,:) = s1(i) == s2;
end
result = any(result);

result = [ 1 1 0 ]

Is there a better way to do this?

Thanks,
Nate

Subject: Comparing individual characters of strings

From: TideMan

Date: 5 Oct, 2011 22:54:34

Message: 2 of 2

On Oct 6, 11:22 am, "Nate " <natejense...@gmail.com> wrote:
> I am trying to compare 2 strings and see if any characters in the first string are in the second string
>
> Here is an example,
> s1 = 'as';
> s2 = 'asd';
>
> result = zeros(length(s1),length(s2));
> for i = 1:length(s1)
>     result(i,:) = s1(i) == s2;
> end
> result = any(result);
>
> result = [ 1 1 0 ]
>
> Is there a better way to do this?
>
> Thanks,
> Nate

Yes, there are better ways.
Matlab has a whole suite of routines for this.
help strcmp
and friends

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us