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:
matrices and index

Subject: matrices and index

From: Konst

Date: 8 Mar, 2011 12:34:04

Message: 1 of 11

I have a vector b=[ 0 0 0 1 1 1 0 0 1 0] and an initial value x=0.5. As I 'read' the vector I want every time that a value in b is > x to increase a variable q=q+1. Then it should nullify it and continue with the next value while putting the qs in a table. I can't describe it exactly so (for example) for the above b I want a table=[3,1].
This is my code:

x=0.5;
q=0;
for i=1:length(b)
    if b(i)>x
        q=q+1
    end
    table(i)=[q]
    q=0
end

which gives me a wrong table. Any thoughts?

Subject: matrices and index

From: Nasser M. Abbasi

Date: 8 Mar, 2011 13:53:40

Message: 2 of 11

On 3/8/2011 4:34 AM, Konst wrote:
> I have a vector b=[ 0 0 0 1 1 1 0 0 1 0] and an initial value x=0.5. As I 'read'
> the vector I want every time that a value in b is> x to increase a variable q=q+1. Then
> it should nullify it and continue with the next value while putting the qs in a table.
> I can't describe it exactly so (for example) for the above b I want a table=[3,1].
> This is my code:
>
> x=0.5;
> q=0;
> for i=1:length(b)
> if b(i)>x
> q=q+1
> end
> table(i)=[q]
> q=0
> end
>
> which gives me a wrong table. Any thoughts?

May be one way:

------------------------
clear all

find_it = @(b) find(diff(b)<0)-find(diff(b)>0); %define function
-----------------------

Now use it:

---------------------------------------
EDU>> b=[ 0 0 0 1 1 1 0 0 1 0 0];
find_it([0 b 0])
ans =
      3 1

EDU>> b=[ 0 0 0 1 1 1 0];
find_it([0 b 0])
ans =
      3


EDU>> b=[ 0 0 0 1 1 1 0 0 1];
find_it([0 b 0])
ans =
      3 1

EDU>> b=[ 1 0 0 1 1 1 0 0 1 0 0 1 1 1];
find_it([0 b 0])
ans =
      1 3 1 3


EDU>> b=[1 1 1 1 1];
find_it([0 b 0])
ans =
      5

EDU>> b=[0 0 0];
find_it([0 b 0])
ans =
    Empty matrix: 1-by-0
---------------------

--Nasser

Subject: matrices and index

From: Konst

Date: 8 Mar, 2011 14:25:28

Message: 3 of 11

"Nasser M. Abbasi" <nma@12000.org> wrote in message <il5cd4$oc6$1@speranza.aioe.org>...
> On 3/8/2011 4:34 AM, Konst wrote:
> > I have a vector b=[ 0 0 0 1 1 1 0 0 1 0] and an initial value x=0.5. As I 'read'
> > the vector I want every time that a value in b is> x to increase a variable q=q+1. Then
> > it should nullify it and continue with the next value while putting the qs in a table.
> > I can't describe it exactly so (for example) for the above b I want a table=[3,1].
> > This is my code:
> >
> > x=0.5;
> > q=0;
> > for i=1:length(b)
> > if b(i)>x
> > q=q+1
> > end
> > table(i)=[q]
> > q=0
> > end
> >
> > which gives me a wrong table. Any thoughts?
>
> May be one way:
>
> ------------------------
> clear all
>
> find_it = @(b) find(diff(b)<0)-find(diff(b)>0); %define function
> -----------------------
>
> Now use it:
>
> ---------------------------------------
> EDU>> b=[ 0 0 0 1 1 1 0 0 1 0 0];
> find_it([0 b 0])
> ans =
> 3 1
>
> EDU>> b=[ 0 0 0 1 1 1 0];
> find_it([0 b 0])
> ans =
> 3
>
>
> EDU>> b=[ 0 0 0 1 1 1 0 0 1];
> find_it([0 b 0])
> ans =
> 3 1
>
> EDU>> b=[ 1 0 0 1 1 1 0 0 1 0 0 1 1 1];
> find_it([0 b 0])
> ans =
> 1 3 1 3
>
>
> EDU>> b=[1 1 1 1 1];
> find_it([0 b 0])
> ans =
> 5
>
> EDU>> b=[0 0 0];
> find_it([0 b 0])
> ans =
> Empty matrix: 1-by-0
> ---------------------
>
> --Nasser
>
>
Thank you!I don't understand how this works exactly but I tried it though and it returns the follow error:
??? Index into matrix is negative or zero. See release notes on changes to
logical indices.
Error in ==> C:\MATLAB6p1\work\aaa.m
On line 10 ==> table=find_it([0 b 0])

Subject: matrices and index

From: Nasser M. Abbasi

Date: 8 Mar, 2011 20:37:01

Message: 4 of 11

On 3/8/2011 6:25 AM, Konst wrote:

>>
> Thank you!I don't understand how this works exactly but I tried it though and it returns the follow error:
> ??? Index into matrix is negative or zero. See release notes on changes to
> logical indices.
> Error in ==> C:\MATLAB6p1\work\aaa.m
> On line 10 ==> table=find_it([0 b 0])

Please post complete code you used.

It works for me, on version 2010a

--Nasser

Subject: matrices and index

From: Nasser M. Abbasi

Date: 8 Mar, 2011 20:45:35

Message: 5 of 11

On 3/8/2011 12:37 PM, Nasser M. Abbasi wrote:
> On 3/8/2011 6:25 AM, Konst wrote:
>
>>>
>> Thank you!I don't understand how this works exactly but I tried it though and it returns the follow error:
>> ??? Index into matrix is negative or zero. See release notes on changes to
>> logical indices.
>> Error in ==> C:\MATLAB6p1\work\aaa.m
>> On line 10 ==> table=find_it([0 b 0])

>
> Please post complete code you used.
>
> It works for me, on version 2010a
>
> --Nasser
>

Here it is again, I justed verified it. It works on 2010a :

---------------------
find_it = @(b) find(diff(b)<0)-find(diff(b)>0);
b=[ 0 0 0 1 1 1 0 0 1 0 0];
table=find_it([0 b 0]);
---------------------

  table
table =
      3 1


--Nasser

Subject: matrices and index

From: Konst

Date: 8 Mar, 2011 21:19:15

Message: 6 of 11

Obviously I have compatibility problems which I'll try to fix. But in order to make this work I have a couple of questions:
First, If i write my code in the order you suggested how will it know the value of b(it returns error about b)?So I tried it the other way round
b=[ 0 0 0 1 1 1 0 0 1 0]
find_it = find(diff(b)<0)-find(diff(b)>0);
table=find_it([0 b 0])
which returned the error I mentioned in my first post.
And second, how do these commands check the values in b in comparison to my initial value x=0.5?
Thank you so much for your time!

Subject: matrices and index

From: Konst

Date: 9 Mar, 2011 15:17:20

Message: 7 of 11

"Konst " <konstance1@hotmail.com> wrote in message <il66gj$34t$1@fred.mathworks.com>...
> Obviously I have compatibility problems which I'll try to fix. But in order to make this work I have a couple of questions:
> First, If i write my code in the order you suggested how will it know the value of b(it returns error about b)?So I tried it the other way round
> b=[ 0 0 0 1 1 1 0 0 1 0]
> find_it = find(diff(b)<0)-find(diff(b)>0);
> table=find_it([0 b 0])
> which returned the error I mentioned in my first post.
> And second, how do these commands check the values in b in comparison to my initial value x=0.5?
> Thank you so much for your time!
Just giving it a nudge

Subject: matrices and index

From: Konst

Date: 10 Mar, 2011 12:13:04

Message: 8 of 11

"Konst " <konstance1@hotmail.com> wrote in message <il85m0$47i$1@fred.mathworks.com>...
> "Konst " <konstance1@hotmail.com> wrote in message <il66gj$34t$1@fred.mathworks.com>...
> > Obviously I have compatibility problems which I'll try to fix. But in order to make this work I have a couple of questions:
> > First, If i write my code in the order you suggested how will it know the value of b(it returns error about b)?So I tried it the other way round
> > b=[ 0 0 0 1 1 1 0 0 1 0]
> > find_it = find(diff(b)<0)-find(diff(b)>0);
> > table=find_it([0 b 0])
> > which returned the error I mentioned in my first post.
> > And second, how do these commands check the values in b in comparison to my initial value x=0.5?
> > Thank you so much for your time!
> Just giving it a nudge
While my previous questions still bother me I tried some other ways like using while instead of if, or set q=0 after every loop and then put each q in a vector but still it doesn't give me the result I want..Any other thoughts would be really welcomed

Subject: matrices and index

From: Nasser M. Abbasi

Date: 10 Mar, 2011 13:05:52

Message: 9 of 11

On 3/9/2011 7:17 AM, Konst wrote:


>> And second, how do these commands check the values in b in comparison to my initial value x=0.5?
>> Thank you so much for your time!

Originally you asked the following:

> I have a vector b=[ 0 0 0 1 1 1 0 0 1 0] and an initial value x=0.5. As I 'read'
> the vector I want every time that a value in b is> x to increase a variable q=q+1.

So, the value x=0.5 itself is not used. As 'b' is either greater or less than 0.5.

If you had different values in the 'b' vector, i.e. not just 1's and 0's, then
maybe the value of x will be relevent.

So, the solution I gave you does not use x=0.5 at all, but looks for change in sign
in the diff result using 'b' as is to locate the size of the clusters of 1's which
is what you wanted.

---------------------------
find_it = @(b) find(diff(b)<0)-find(diff(b)>0); %define function
b=[ 0 0 0 1 1 1 0 0 1 0 0];
find_it([0 b 0])
ans =
       3 1
---------------------------

HTH,

--Nasser

Subject: matrices and index

From: Konst

Date: 10 Mar, 2011 14:30:09

Message: 10 of 11

I also tried sth like this but still it doesn't give me the result I want:
q=0
j=1
for i=1:length(b)
    if b(i)>xth
    q=q+1;
    end
end
for j=1:i
table(j)=[qk]
end
I really can't figure this one out!

Subject: matrices and index

From: Konst

Date: 14 Mar, 2011 21:14:06

Message: 11 of 11

If anyone has the same problem I managed to solve it like this:
for i=1:length(b)
    if b(i)<x
        q(i)=0;
    else
        q(i)=q(i-1)+1;
    end
end
for i=2:length(q)
    if q(i-1)>q(i)
        table(i)=q(i-1);
    end
end
l=table(table~=0)

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