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:
Removing Zero and its indices

Subject: Removing Zero and its indices

From: kk aKI

Date: 12 Feb, 2010 20:26:20

Message: 1 of 9

Suppose I have vector
 t =[1:2:1000]
and
s=[2 3 6 0 2 4 ........]

Now i want to remove all zeros from s and want to plot it with its indices in t i:e plot(t,s). How can i do it.

I tried
s=s(s~=0)

It removes all zero but i dont know which indices form t i can use in plotting.

Thanks

kk

Subject: Removing Zero and its indices

From: dpb

Date: 12 Feb, 2010 20:31:41

Message: 2 of 9

kk aKI wrote:
> Suppose I have vector
> t =[1:2:1000]
> and
> s=[2 3 6 0 2 4 ........]
>
> Now i want to remove all zeros from s and want to plot it with its
> indices in t i:e plot(t,s). How can i do it.
>
> I tried
> s=s(s~=0)
>
> It removes all zero but i dont know which indices form t i can use in
> plotting.


The indices are those corresponding to s==0

See find() for functional form

--

Subject: Removing Zero and its indices

From: dpb

Date: 12 Feb, 2010 20:37:07

Message: 3 of 9

dpb wrote:
...

>> It removes all zero but i dont know which indices form t i can use in
>> plotting.
> The indices are those corresponding to s==0
...
Sorry for typo...that's ~= to match, of course.

Consider your shortened example

 >> s=[2 3 6 0 2 4 ];t =[1:2:length(s)*2];
 >> t=t(s~=0)
t =
      1 3 5 9 11
 >>

--

Subject: Removing Zero and its indices

From: Rami AbouSleiman

Date: 12 Feb, 2010 20:40:06

Message: 4 of 9

dpb <none@non.net> wrote in message <hl4dsv$uvi$1@news.eternal-september.org>...
> kk aKI wrote:
> > Suppose I have vector
> > t =[1:2:1000]
> > and
> > s=[2 3 6 0 2 4 ........]
> >
> > Now i want to remove all zeros from s and want to plot it with its
> > indices in t i:e plot(t,s). How can i do it.
> >
> > I tried
> > s=s(s~=0)
> >
> > It removes all zero but i dont know which indices form t i can use in
> > plotting.
>
>
> The indices are those corresponding to s==0
>
> See find() for functional form
>
> --try to put it in a for loop

Subject: Removing Zero and its indices

From: ImageAnalyst

Date: 12 Feb, 2010 20:42:44

Message: 5 of 9

One way to do it:

t = [1 3 5 7 9 11 13 15 17 19]
s = [2 3 6 0 2 4 0 3 6 0]
indexesOfZeros = (s == 0)
t(indexesOfZeros) = [] % Get rid of 7, 13, 19
s(indexesOfZeros) = [] % Get rid of 0's
% Now do your plot with your new t and s.

Subject: Removing Zero and its indices

From: dpb

Date: 12 Feb, 2010 22:00:58

Message: 6 of 9

Rami AbouSleiman wrote:
...

>> --try to put it in a for loop

for not needed... :)

--

Subject: Removing Zero and its indices

From: kk aKI

Date: 12 Feb, 2010 22:27:21

Message: 7 of 9

I think its working for me now, Thanks to every one. And, I know how to use for loop but it makes the programe more n more slow. I am trying it to make fast
dpb <none@non.net> wrote in message <hl4j4c$li3$1@news.eternal-september.org>...
> Rami AbouSleiman wrote:
> ...
>
> >> --try to put it in a for loop
>
> for not needed... :)
>
> --

Subject: Removing Zero and its indices

From: kk aKI

Date: 13 Feb, 2010 08:59:22

Message: 8 of 9

"kk aKI" <akikumar1983@gmail.com> wrote in message <hl4kk9$alm$1@fred.mathworks.com>...
> I think its working for me now, Thanks to every one. And, I know how to use for loop but it makes the programe more n more slow. I am trying it to make fast
> dpb <none@non.net> wrote in message <hl4j4c$li3$1@news.eternal-september.org>...
> > Rami AbouSleiman wrote:
> > ...
> >
> > >> --try to put it in a for loop
> >
> > for not needed... :)
> >
> > --
 And how this works with rand function suppose i want to remove any 50% of the data randomly from vector S same from vector t and plot them as plot(S,t). Really thanks, My code really speed up after i am doing vectorization instead of for loop thanks

kk

Subject: Removing Zero and its indices

From: ImageAnalyst

Date: 13 Feb, 2010 12:53:51

Message: 9 of 9

On Feb 13, 3:59 am, "kk aKI" <akikumar1...@gmail.com> wrote:
>  And how this works with rand function suppose i want to remove any 50% of the data randomly from vector  S same from vector t and plot them as plot(S,t). Really thanks, My code really speed up after i am doing vectorization instead of for loop thanks
>
> kk
---------------------------------------------------------------------------------------------------------

t = [1 3 5 7 9 11 13 15 17 19]
s = [2 3 6 0 2 4 0 3 6 0]
indexesOfZeros = (s == 0)
t(indexesOfZeros) = [] % Get rid of 7, 13, 19
s(indexesOfZeros) = [] % Get rid of 0's

numberOfIndexes = length(t)
scrambledIndexes = randperm(numberOfIndexes)
halfOfTheIndexes = scrambledIndexes(1:floor(length(scrambledIndexes)/
2));
halfOfTheIndexes = sort(halfOfTheIndexes)
tHalf = t(halfOfTheIndexes)
sHalf = s(halfOfTheIndexes)
plot(sHalf, tHalf)
xlabel('sHalf');
ylabel('tHalf');

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