Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Mathematics
General TopicsResearchOperations ResearchStatisticsMathematical LogicNumerical AnalysisUndergraduate MathAlgebra HelpRecreational Math
Math Software
MapleMathematicaMATLABScilabSASSPSS

Math Forum / Math Software / MATLAB / October 2008



Tip: Looking for answers? Try searching our database.

union keeping track of indices

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Prime Mover - 31 Oct 2008 12:15 GMT
Having two matrices a and b like in the example below:

a =

    3     3    -5
    4     5    -6
    4     4    -9
    4     5    -3

b =

    4     4    -2
    4     5    -4
    8     7   -15
    9     0    -1

I need to get an output like this:

    3     3     -5    NaN
    4     4     -9      -2
    4     5     -3      -4
    8     7    NaN   -15
    9     0    NaN   -1

That is, I have to perform a union based on the first two colors of a
and b and keep track of the original indices so as to be able to
retrieve the original values in their third columns, and write them in
the last two colums of the output matrix.

I know the command union can perform the union task on the matrices
but the doc help file says:

"If a value appears in both a and b, union indexes its occurrence in
b. If a value appears more than once in b or in a (but not in b),
union indexes the last occurrence of the value."

Therefore, after union I can know from what rows in the matrix b the
united values come from, but not from matrix a.

Once again, thank you for the cooperation.
Jos - 31 Oct 2008 12:41 GMT
> Having two matrices a and b like in the example below:
>
[quoted text clipped - 36 lines]
>
> Once again, thank you for the cooperation.


Based on what logic do you keep the value -3 in a for [4 5] and not -6?

Jos
Prime Mover - 31 Oct 2008 14:13 GMT
> > Having two matrices a and b like in the example below:
>
[quoted text clipped - 42 lines]
>
> - Mostrar texto entre aspas -

Ooops, I am sorry Jos! Just ignore the [4 5 -6] line in matrix a . So,
a correct example of the task would be:

a =

    3     3    -5
    4     4    -9
    4     5    -3

b =

    4     4    -2
    4     5    -4
    8     7   -15
    9     0    -1

I need to get an output like this:

    3     3     -5    NaN
    4     4     -9      -2
    4     5     -3      -4
    8     7    NaN   -15
    9     0    NaN   -1

----------
Thanks for the interest and sorry again for the confusion.
Jos - 31 Oct 2008 14:28 GMT
> > Prime Mover <eple...@hotmail.com> wrote in message <0685b44e-9fbb-4ac0-a8=
> a4-0e2900ce0...@u18g2000pro.googlegroups.com>...
> >
> > > Having two matrices a and b like in the example below:
< snip ...
> So,
> a correct example of the task would be:
[quoted text clipped - 20 lines]
> ----------
> Thanks for the interest and sorry again for the confusion.

Something like this, perhaps:

uab = union(a(:,1:2),b(:,1:2),'rows')
tfa = ismember(uab,a(:,1:2),'rows')
tfb = ismember(uab,b(:,1:2),'rows')
uab(:,3:4) = NaN
uab(tfa,3) = a(:,3)
uab(tfb,4) = b(:,3)

hth
Jos
Prime Mover - 31 Oct 2008 19:40 GMT
> > > Prime Mover <eple...@hotmail.com> wrote in message <0685b44e-9fbb-4ac0-a8=
> > a4-0e2900ce0...@u18g2000pro.googlegroups.com>...
[quoted text clipped - 39 lines]
>
> - Mostrar texto entre aspas -

Yes, I was looking for a way in that you don't need to lookup in the
original matrices again to find the desired indices, so as to reduce
processing time. I have matrices with thousands of rows.

Thank you.
Bruno Luong - 31 Oct 2008 21:05 GMT
% Data
a =[ 3 3 -5;
    4 5 -6;
    4 4 -9;
    4 5 -3 ]

b =[ 4 4 -2;
    4 5 -4;
    8 7 -15;
    9 0 -1 ]

% Engine
acolor=a(:,[1 2]);
bcolor=b(:,[1 2]);

[trast ib] = unique([acolor; bcolor], 'rows');
[trast ia] = unique([bcolor; acolor], 'rows');

ib=ib-size(a,1);
ia=ia-size(b,1);
c=nan(length(ia),4);
t=ia>0; c(t,[1 2 3])=a(ia(t),:);
t=ib>0; c(t,[1 2 4])=b(ib(t),:);

% Check
c

% Bruno
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.