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:
Replace one or more occurrences of a certain value with the value preceding the first occurrence

Subject: Replace one or more occurrences of a certain value with the value preceding the first occurrence

From: William Frane

Date: 8 Dec, 2011 21:06:08

Message: 1 of 8

Hello,

I'm wondering if there's a nice MATLAB-esque (i.e., not involving any for loops) way to replace one or more occurrences of a particular value in a vector with the value immediately preceding the first occurrence. For example, given the vector

[1 2 3 -1 -1 6 7 -1 9 10 -1 -1 -1 -1 15]

and with -1 specified as the value to replace, the desired result would be:

[1 2 3 3 3 6 7 7 9 10 10 10 10 10 15]

Any suggestions would be appreciated.

WFrane

Subject: Replace one or more occurrences of a certain value with the value

From: dpb

Date: 8 Dec, 2011 21:13:05

Message: 2 of 8

On 12/8/2011 3:06 PM, William Frane wrote:
> Hello,
>
> I'm wondering if there's a nice MATLAB-esque (i.e., not involving any
> for loops) way to replace one or more occurrences of a particular value
> in a vector with the value immediately preceding the first occurrence.
> For example, given the vector
>
> [1 2 3 -1 -1 6 7 -1 9 10 -1 -1 -1 -1 15]
>
> and with -1 specified as the value to replace, the desired result would be:
>
> [1 2 3 3 3 6 7 7 9 10 10 10 10 10 15]
>
> Any suggestions would be appreciated.

Don't see any way w/ _no_ loops. You can find the locations and the
leading values by a combination of find(), diff() and indexing but then
you'll have to work through the number of sequences (3 in the example)
to do the substitution(s) by any idea that comes to me at the moment.

I'd wrap the logic in a function then I don't have to look at the
loop...(therefore it isn't there! :) )

--

Subject: Replace one or more occurrences of a certain value with the value preceding the first occurrence

From: Matt J

Date: 8 Dec, 2011 22:22:09

Message: 3 of 8

"William Frane" wrote in message <jbr8s0$3j5$1@newscl01ah.mathworks.com>...
> Hello,
>
> I'm wondering if there's a nice MATLAB-esque (i.e., not involving any for loops) way to replace one or more occurrences of a particular value in a vector with the value immediately preceding the first occurrence. For example, given the vector
>
> [1 2 3 -1 -1 6 7 -1 9 10 -1 -1 -1 -1 15]
>
> and with -1 specified as the value to replace, the desired result would be:
>
> [1 2 3 3 3 6 7 7 9 10 10 10 10 10 15]
=======================

Well, I don't know if I would recommend doing it this way, but if you have the image processing toolbox, the following is a loop-free method:



a=[1 2 3 -1 -1 6 7 -1 9 10 -1 -1 -1 -1 15]

idx=(a==-1);
 L=bwlabel(idx);
[u,i,j]=unique(L,'first');

mask=[0 a(i(2:end)-1)];

anew=a.*(~idx)+mask(j)

Subject: Replace one or more occurrences of a certain value with the value preceding the first occurrence

From: Bruno Luong

Date: 9 Dec, 2011 00:34:08

Message: 4 of 8

"William Frane" wrote in message <jbr8s0$3j5$1@newscl01ah.mathworks.com>...
> Hello,
>
> I'm wondering if there's a nice MATLAB-esque (i.e., not involving any for loops) way to replace one or more occurrences of a particular value in a vector with the value immediately preceding the first occurrence. For example, given the vector
>
> [1 2 3 -1 -1 6 7 -1 9 10 -1 -1 -1 -1 15]
>
> and with -1 specified as the value to replace, the desired result would be:
>
> [1 2 3 3 3 6 7 7 9 10 10 10 10 10 15]
>
> Any suggestions would be appreciated.
>

This should work:

b = A==-1; % value -1 to be replaced
B = A(~b);
[~, loc]=histc(find(b),[find(~b) Inf]);
A(b) = B(loc)

% Bruno

Subject: Replace one or more occurrences of a certain value with the value preceding the first occurrence

From: William Frane

Date: 9 Dec, 2011 14:39:08

Message: 5 of 8

Thanks for the suggestions, everyone. I think I'll go with Bruno's histc method, although Matt's image processing method is interesting as well.

WFrane

Subject: Replace one or more occurrences of a certain value with the value preceding the first occurrence

From: Matt J

Date: 9 Dec, 2011 15:13:08

Message: 6 of 8

"William Frane" wrote in message <jbt6ic$5ik$1@newscl01ah.mathworks.com>...
>
> Thanks for the suggestions, everyone. I think I'll go with Bruno's histc method, although Matt's image processing method is interesting as well.
===============

I tend to think, though, that a for-loop will beat either of these suggestions in terms of speed.

Subject: Replace one or more occurrences of a certain value with the value preceding the first occurrence

From: Matt J

Date: 9 Dec, 2011 15:39:08

Message: 7 of 8

"Matt J" wrote in message <jbt8i4$cuq$1@newscl01ah.mathworks.com>...
> "William Frane" wrote in message <jbt6ic$5ik$1@newscl01ah.mathworks.com>...
> >
> > Thanks for the suggestions, everyone. I think I'll go with Bruno's histc method, although Matt's image processing method is interesting as well.
> ===============
>
> I tend to think, though, that a for-loop will beat either of these suggestions in terms of speed.
===========

So maybe something like this:

A=[1 2 3 -1 -1 6 7 -1 9 10 -1 -1 -1 -1 15];
b = [0,A==-1,0];


starts=strfind(b,[0 1])-1;
stops=strfind(b,[1,0])-1;

for ii=1:length(starts)
           
     A(starts(ii)+1:stops(ii))=A(starts(ii));

end


A,

Subject: Replace one or more occurrences of a certain value with the value preceding the first occurrence

From: Roger Stafford

Date: 9 Dec, 2011 16:46:09

Message: 8 of 8

"William Frane" wrote in message <jbr8s0$3j5$1@newscl01ah.mathworks.com>...
> I'm wondering if there's a nice MATLAB-esque (i.e., not involving any for loops) way to replace one or more occurrences of a particular value in a vector with the value immediately preceding the first occurrence. For example, given the vector
>
> [1 2 3 -1 -1 6 7 -1 9 10 -1 -1 -1 -1 15]
>
> and with -1 specified as the value to replace, the desired result would be:
>
> [1 2 3 3 3 6 7 7 9 10 10 10 10 10 15]
- - - - - - - - -
  I agree with Matt J. A simple for-loop may very well be the best solution. The following does a single pass through the vector 'x'.

  You didn't specify what is to happen if -1 occurs initially. This code leaves 'x' unchanged until a different element is encountered.

 x0 = -1;
 for k = 1:length(x)
  if x(k) == -1
   x(k) = x0;
  else
   x0 = x(k);
  end
 end

Roger Stafford

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