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:
indexing iteration

Subject: indexing iteration

From: Raymond

Date: 30 Sep, 2010 15:03:21

Message: 1 of 8

My function has two string inputs and an output showing the location. Examples:

out=wordfinder('mom is momentarily moot', 'mo')
and the out should then be [1 8 20]

out=wordfinder(' ', 'ssa')
out should then be [ ]

out=wordfinder('never believe NEW Nations', 'n')
out should then be [1 15 19]

Realize this function doesn't discriminate by case. This is what I've got so far:

function [c] = wordfinder(a, b)
for Position = 1:length(b);
    if strcmpi(a(Position:Position+length(b)-1), b)==true;
        c = index;
    else
        c =[];
    end
 
end

I think everything is correct except the strcmpi line. I can't figure out for the life of me how to compare the two strings and to show all positions where it occurs. Please try to help me fix this code. I don't want to do it any other way, because I'm trying to learn iteration through this. Thanks!

Subject: indexing iteration

From: Sean

Date: 30 Sep, 2010 15:22:05

Message: 2 of 8

"Raymond " <atlantaswagsurfers3@yahoo.com> wrote in message <i828rp$mli$1@fred.mathworks.com>...
> My function has two string inputs and an output showing the location. Examples:
>
> out=wordfinder('mom is momentarily moot', 'mo')
> and the out should then be [1 8 20]
>
> out=wordfinder(' ', 'ssa')
> out should then be [ ]
>
> out=wordfinder('never believe NEW Nations', 'n')
> out should then be [1 15 19]
>
> Realize this function doesn't discriminate by case. This is what I've got so far:
>
> function [c] = wordfinder(a, b)
> for Position = 1:length(b);
> if strcmpi(a(Position:Position+length(b)-1), b)==true;
> c = index;
> else
> c =[];
> end
>
> end
>
> I think everything is correct except the strcmpi line. I can't figure out for the life of me how to compare the two strings and to show all positions where it occurs. Please try to help me fix this code. I don't want to do it any other way, because I'm trying to learn iteration through this. Thanks!

No the bad line is the initiation of your for-loop. You want to traverse all os a, not b. Also, in your else statement every time it fails you delete all of the data yo already learned. So let's say it triggers the 'mo' in mom, when it tries 'om' and fails the previous data (c) is set to [].

function [c] = wordfinder(a, b)
c = false(size(a)); %Every possible point
for Position = 1:length(a);
    if strcmpi(a(Position:Position+length(b)-1), b);
        c(Position) = true; %set point to true
    end
end
c = find(c); %find the true points.

Or you could do it this way:
function [c] = wordfinder(a, b)
c = [];
for Position = 1:length(a);
    if strcmpi(a(Position:Position+length(b)-1), b);
        c = [c, Position];
    end
end

But that's bad programming practice since it involves growing the array c in a for loop.

Subject: indexing iteration

From: Matt Fig

Date: 30 Sep, 2010 15:32:08

Message: 3 of 8

Do you know about REGEXP?

regexpi('mom is momentarily moot', 'mo')
regexp(' ', 'ssa')
regexpi('never believe NEW Nations', 'n')

Subject: indexing iteration

From: Raymond

Date: 30 Sep, 2010 15:35:08

Message: 4 of 8

Right...I see my problem. However, wouldn't the matrix dimensions not agree nevertheless if you added more than one letter to b? So then 'mo' would not work.
"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i829ut$53b$1@fred.mathworks.com>...
> "Raymond " <atlantaswagsurfers3@yahoo.com> wrote in message <i828rp$mli$1@fred.mathworks.com>...
> > My function has two string inputs and an output showing the location. Examples:
> >
> > out=wordfinder('mom is momentarily moot', 'mo')
> > and the out should then be [1 8 20]
> >
> > out=wordfinder(' ', 'ssa')
> > out should then be [ ]
> >
> > out=wordfinder('never believe NEW Nations', 'n')
> > out should then be [1 15 19]
> >
> > Realize this function doesn't discriminate by case. This is what I've got so far:
> >
> > function [c] = wordfinder(a, b)
> > for Position = 1:length(b);
> > if strcmpi(a(Position:Position+length(b)-1), b)==true;
> > c = index;
> > else
> > c =[];
> > end
> >
> > end
> >
> > I think everything is correct except the strcmpi line. I can't figure out for the life of me how to compare the two strings and to show all positions where it occurs. Please try to help me fix this code. I don't want to do it any other way, because I'm trying to learn iteration through this. Thanks!
>
> No the bad line is the initiation of your for-loop. You want to traverse all os a, not b. Also, in your else statement every time it fails you delete all of the data yo already learned. So let's say it triggers the 'mo' in mom, when it tries 'om' and fails the previous data (c) is set to [].
>
> function [c] = wordfinder(a, b)
> c = false(size(a)); %Every possible point
> for Position = 1:length(a);
> if strcmpi(a(Position:Position+length(b)-1), b);
> c(Position) = true; %set point to true
> end
> end
> c = find(c); %find the true points.
>
> Or you could do it this way:
> function [c] = wordfinder(a, b)
> c = [];
> for Position = 1:length(a);
> if strcmpi(a(Position:Position+length(b)-1), b);
> c = [c, Position];
> end
> end
>
> But that's bad programming practice since it involves growing the array c in a for loop.

Subject: indexing iteration

From: Sean

Date: 30 Sep, 2010 15:42:07

Message: 5 of 8

"Matt Fig" <spamanon@yahoo.com> wrote in message <i82aho$dn6$1@fred.mathworks.com>...
> Do you know about REGEXP?
>
> regexpi('mom is momentarily moot', 'mo')
> regexp(' ', 'ssa')
> regexpi('never believe NEW Nations', 'n')

Also:
>>strfind('mom is momentarily moot', 'mo')

However, I think the OP wants to learn how to use indexing, for which this is a really nice little exercise!

Subject: indexing iteration

From: Sean

Date: 30 Sep, 2010 15:46:20

Message: 6 of 8

.
> Right...I see my problem. However, wouldn't the matrix dimensions not agree nevertheless if you added more than one letter to b? So then 'mo' would not work.

Of course it will. How are you going to circumvent this?

('mom is momentarily moot', 'mo')

Since you know 'mo' is two letters long, the last letter in 'a' cannot possibly be the beginning of a two letter expression.
So now in your for-loop initiation how to you stop the for-loop from testing the last letter of 'a'?

Subject: indexing iteration

From: Matt Fig

Date: 30 Sep, 2010 16:07:22

Message: 7 of 8

Oh, I didn't see the OP's last line. Sorry. I do not think you want to iterate over all a. For example:


function [c] = wordfinder(a, b)
c = zeros(size(a)); % Perhaps only: floor(length(a)/length(b)) is needed.
L = length(b);
cnt = 0;

for pos = 1:length(a)-L
    idx = strcmpi(a(pos:pos+L-1), b); % Is it a match?
    if idx
        cnt = cnt + 1; % The position in c.
        c(cnt) = pos; % The current index into a.
    end
end

c = c(1:cnt); % Take only part of c.

Subject: indexing iteration

From: Matt Fig

Date: 30 Sep, 2010 16:22:21

Message: 8 of 8

"Matt Fig" <spamanon@yahoo.com> wrote in message <i82cjq$srq$1@fred.mathworks.com>...
> Oh, I didn't see the OP's last line. Sorry. I do not think you want to iterate over all a. For example:
>
>
> function [c] = wordfinder(a, b)
> c = zeros(size(a)); % Perhaps only: floor(length(a)/length(b)) is needed.
> L = length(b);
> cnt = 0;
>
> for pos = 1:length(a)-L


Make that 1:length(b)-L+1, duh.

Tags for this Thread

No tags are associated with 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