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:
Build a new vector using relational query within a script

Subject: Build a new vector using relational query within a script

From: Megan

Date: 25 Jun, 2010 23:48:05

Message: 1 of 5

Hi everyone,

I can't seem to find a better way of tackling the following so that it is quicker.

What I essentially want to do is to conduct a relational query that is simple to do in Access, but I need the output to be built over every timestep in my script. I can only seem to find information about the Visual Query Builder, which I don't think will help me in this case.
My initial matrix looks something like this:

A = [1,0;
        2,1;
        3,0]

which is by nature of the script split into two vectors:

a = [1;
       2;
       3]

b = [0;
       1;
       0]

I need the contents of A(:,2) with indices A(:,1) to be matched to the corresponding indices of a new vector B so that it looks like this when it is done:

B = [1,0;
       1,0;
       2,1;
       2,1;
       2,1;
       3,0;
       3,0]

where c = B(:,1)

The function I currently use to do this is as follows, but is really slow for an A matrix of 1.1 million rows, and a B matrix of 5 million rows. Any (urgent!) help is much appreciated!

.......................
function pu_sparse_TU = create_pu_sparse_TU(a,b,c)

pu_sparse_TU = zeros(length(c(:,1)),1);


h = 1;


for j = 1:length(b(:,1));
    while c(h,1)==b(j,1);
        pu_sparse_TU(h,1)=a(j);
        h = h + 1;
        if h > length(c(:,1));
            break
        end
    end
end

Subject: Build a new vector using relational query within a script

From: Matt Fig

Date: 26 Jun, 2010 01:08:03

Message: 2 of 5

Your description is a little confused. It seems like you say that you start out with matrix A, which gives you a and b, then use those to get to B, which then gives you c. Then it seems you use a,b, and c as inputs in your function. But it also seems like you are saying that your function gives you B, in which case you need c to get B which gets you c - which is awkward at least.

Which is it?
I see how a and b are related to A, but how are they related to B?
Is B the matrix you are looking for, or is it a given as is A?
If B is the return value from your function, then what about input argument c?

Subject: Build a new vector using relational query within a script

From: Roger Stafford

Date: 26 Jun, 2010 01:40:07

Message: 3 of 5

"Megan " <m.evans1@uq.edu.au> wrote in message <i03f7l$42e$1@fred.mathworks.com>...
> Hi everyone,
>
> I can't seem to find a better way of tackling the following so that it is quicker.
>
> What I essentially want to do is to conduct a relational query that is simple to do in Access, but I need the output to be built over every timestep in my script. I can only seem to find information about the Visual Query Builder, which I don't think will help me in this case.
> My initial matrix looks something like this:
>
> A = [1,0;
> 2,1;
> 3,0]
>
> which is by nature of the script split into two vectors:
>
> a = [1;
> 2;
> 3]
>
> b = [0;
> 1;
> 0]
>
> I need the contents of A(:,2) with indices A(:,1) to be matched to the corresponding indices of a new vector B so that it looks like this when it is done:
>
> B = [1,0;
> 1,0;
> 2,1;
> 2,1;
> 2,1;
> 3,0;
> 3,0]
>
> where c = B(:,1)
>
> The function I currently use to do this is as follows, but is really slow for an A matrix of 1.1 million rows, and a B matrix of 5 million rows. Any (urgent!) help is much appreciated!
>
> .......................
> function pu_sparse_TU = create_pu_sparse_TU(a,b,c)
>
> pu_sparse_TU = zeros(length(c(:,1)),1);
>
>
> h = 1;
>
>
> for j = 1:length(b(:,1));
> while c(h,1)==b(j,1);
> pu_sparse_TU(h,1)=a(j);
> h = h + 1;
> if h > length(c(:,1));
> break
> end
> end
> end
- - - - - - - - - -
  Is this what you have in mind:

 B = [a(c),b(c)];

Roger Stafford

Subject: Build a new vector using relational query within a script

From: Roger Stafford

Date: 26 Jun, 2010 03:49:04

Message: 4 of 5

"Megan " <m.evans1@uq.edu.au> wrote in message <i03f7l$42e$1@fred.mathworks.com>...
> .......
> where c = B(:,1)
> ......
> while c(h,1)==b(j,1);
> .....
- - - - - - - - -
  After taking a more careful look at your "create_pu_sparse_TU" function, I appear to have misunderstood what your c vector is. When you stated

 c = B(:,1)

I think you meant c = B(:,2). The values of c in the function are apparently the 0's and 1's that are found in b = A(:,2), as evidenced by the condition "c(h,1)==b(j,1)" and not the indices that are found in a = A(:,1), so my code wouldn't work.

  For your function to make any sense, the b vector must alternate at each row and the c vector must be expansions of each of these single values. That is, in the example you gave, b = [0;1;0] and c = [0;0;1;1;1;0;0] then consists solely of various numbers of repetitions of each element in b, two of the first, three of the second, and two of the third. If this is the case, there is no need to even access b since it has to be assumed to alternate at each row.

  It is possible to write code which accomplishes this in one line, based on that assumption:

 d = a(cumsum([true;diff(c)~=0]));

  However, I question the appropriateness of doing it this way. The very process that allowed you to do such an expansion of b into c is very likely to have provided you with a much better way of producing the required indices in vector d.

  I think you need to rethink your explanation of what you are trying to do so that it can make better sense to others of us in this newsgroup.

Roger Stafford

Subject: Build a new vector using relational query within a script

From: Megan

Date: 26 Jun, 2010 04:19:05

Message: 5 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i03tbg$8fo$1@fred.mathworks.com>...
> "Megan " <m.evans1@uq.edu.au> wrote in message <i03f7l$42e$1@fred.mathworks.com>...
> > .......
> > where c = B(:,1)
> > ......
> > while c(h,1)==b(j,1);
> > .....
> - - - - - - - - -
> After taking a more careful look at your "create_pu_sparse_TU" function, I appear to have misunderstood what your c vector is. When you stated
>
> c = B(:,1)
>
> I think you meant c = B(:,2). The values of c in the function are apparently the 0's and 1's that are found in b = A(:,2), as evidenced by the condition "c(h,1)==b(j,1)" and not the indices that are found in a = A(:,1), so my code wouldn't work.
>
> For your function to make any sense, the b vector must alternate at each row and the c vector must be expansions of each of these single values. That is, in the example you gave, b = [0;1;0] and c = [0;0;1;1;1;0;0] then consists solely of various numbers of repetitions of each element in b, two of the first, three of the second, and two of the third. If this is the case, there is no need to even access b since it has to be assumed to alternate at each row.
>
> It is possible to write code which accomplishes this in one line, based on that assumption:
>
> d = a(cumsum([true;diff(c)~=0]));
>
> However, I question the appropriateness of doing it this way. The very process that allowed you to do such an expansion of b into c is very likely to have provided you with a much better way of producing the required indices in vector d.
>
> I think you need to rethink your explanation of what you are trying to do so that it can make better sense to others of us in this newsgroup.
>
> Roger Stafford

Hi Roger and Matt,

I used the A and B matrices to indicate how such an operation would play out using Access, but it seems to have made things more confusing. I do actually mean c = B(:,1), not c = B(:,2) - but let's ignore A and B since a,b and c are the inputs I"m interested in. The output of my function is pu_sparse_TU.
So a = indices of interest, b = corresponding values, c = indices of interest but expanded (part of a sparse matrix), and the indices in c don't repeat in a predictable pattern. I want the values in b to be expanded according to c, following the matching rules between a and b.

Cheers, Megan

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