Got Questions? Get Answers.
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:
counting previous zero entry

Subject: counting previous zero entry

From: Peerapat V.Chareon

Date: 10 May, 2011 04:02:05

Message: 1 of 3

I am trying to obtain logical output which is that its immediate previous entry is zero. I'd like to avoid using FOR loop since the size of the input matrix is quite large. But I just couldn't figure out how.

Here is the example of my input and desired output.
input = [6 5 0 0 8 2; 3 5 0 8 0 9 ]
output = [0 0 0 0 1 0; 0 0 0 1 0 1]

Any suggestions would be really appreciated.

Subject: counting previous zero entry

From: Nasser M. Abbasi

Date: 10 May, 2011 04:29:38

Message: 2 of 3

On 5/9/2011 9:02 PM, Peerapat V.Chareon wrote:
> I am trying to obtain logical output which is that its immediate previous
> entry is zero. I'd like to avoid using FOR loop since the size of the
>input matrix is quite large. But I just couldn't figure out how.
>
> Here is the example of my input and desired output.
> input = [6 5 0 0 8 2; 3 5 0 8 0 9 ]
> output = [0 0 0 0 1 0; 0 0 0 1 0 1]
>
> Any suggestions would be really appreciated.

it is not clear.

SO,

IN=[6 5 0 0 8 2]
OU=[0 0 0 0 1 0]

So, you zero verything after the '8'? or like this:

IN=[6 5 0 0 8 2 0 5 2 0 1]
OU=[1 1 0 0 1 0 0 1 0 0 1]

?

--Nasser

Subject: counting previous zero entry

From: Roger Stafford

Date: 10 May, 2011 05:17:05

Message: 3 of 3

"Peerapat V.Chareon" <peerapat@student.unsw.edu.au> wrote in message <iqadbt$p20$1@newscl01ah.mathworks.com>...
> I am trying to obtain logical output which is that its immediate previous entry is zero. I'd like to avoid using FOR loop since the size of the input matrix is quite large. But I just couldn't figure out how.
>
> Here is the example of my input and desired output.
> input = [6 5 0 0 8 2; 3 5 0 8 0 9 ]
> output = [0 0 0 0 1 0; 0 0 0 1 0 1]
>
> Any suggestions would be really appreciated.
- - - - - - - - - - -
  In your example you only have ones when the previous element in 'input' is zero and the current element is nonzero, so that is how the following code works.

 prev = [ones(size(input,1),1),input(:,1:end-1)];
 output = prev==0 & input~=0

Roger Stafford

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