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:
Keeping track of elements in a merged array

Subject: Keeping track of elements in a merged array

From: Mayuri

Date: 13 Mar, 2012 04:03:21

Message: 1 of 4

Hello,
   I would like to do the following :-
I have 2 arrays A and B of size (1,n) and (1,m) where m <n . I want to merge the 2 arrays and arrange the elements in ascending order. For this I thought I would use the functions horzcat and sort to do this. My problem starts here.

  I want to know whether each element of the final array came from array A or B initially. In some sense I need to tag the elements of arrays A and B and these should be retained even after merging and sorting. How do I proceed?

- Thanks

Subject: Keeping track of elements in a merged array

From: Roger Stafford

Date: 13 Mar, 2012 04:21:13

Message: 2 of 4

"Mayuri " <mayuris@rri.res.in> wrote in message <jjmgu9$i3h$1@newscl01ah.mathworks.com>...
> Hello,
> I would like to do the following :-
> I have 2 arrays A and B of size (1,n) and (1,m) where m <n . I want to merge the 2 arrays and arrange the elements in ascending order. For this I thought I would use the functions horzcat and sort to do this. My problem starts here.
>
> I want to know whether each element of the final array came from array A or B initially. In some sense I need to tag the elements of arrays A and B and these should be retained even after merging and sorting. How do I proceed?
>
> - Thanks
- - - - - - - - -
  That's where the second argument returned from 'sort' comes in handy.

 [C,p] = sort([A,B]);
 t = (p<=length(A));

The quantity 't' is a logical vector the same size as C. It is true when the corresponding element of C came from A and false when it came from B.

Roger Stafford

Subject: Keeping track of elements in a merged array

From: Mayuri

Date: 13 Mar, 2012 05:04:15

Message: 3 of 4

Thank you very much. This is exactly what I wanted. Where can I find information to better understand hoe the line
  t = (p<=length(A));
works?

"Roger Stafford" wrote in message <jjmhvp$l71$1@newscl01ah.mathworks.com>...
> "Mayuri " <mayuris@rri.res.in> wrote in message <jjmgu9$i3h$1@newscl01ah.mathworks.com>...
> > Hello,
> > I would like to do the following :-
> > I have 2 arrays A and B of size (1,n) and (1,m) where m <n . I want to merge the 2 arrays and arrange the elements in ascending order. For this I thought I would use the functions horzcat and sort to do this. My problem starts here.
> >
> > I want to know whether each element of the final array came from array A or B initially. In some sense I need to tag the elements of arrays A and B and these should be retained even after merging and sorting. How do I proceed?
> >
> > - Thanks
> - - - - - - - - -
> That's where the second argument returned from 'sort' comes in handy.
>
> [C,p] = sort([A,B]);
> t = (p<=length(A));
>
> The quantity 't' is a logical vector the same size as C. It is true when the corresponding element of C came from A and false when it came from B.
>
> Roger Stafford

Subject: Keeping track of elements in a merged array

From: Roger Stafford

Date: 13 Mar, 2012 06:21:13

Message: 4 of 4

"Mayuri " <mayuris@rri.res.in> wrote in message <jjmkgf$s2l$1@newscl01ah.mathworks.com>...
> Thank you very much. This is exactly what I wanted. Where can I find information to better understand hoe the line
> t = (p<=length(A));
> works?
- - - - - - - - -
  First look up in your "Matlab Programming Fundamentals" or similar documents all the references to 1) the logical class, 2) logical operators, 3) logical arrays, 4) relational operators, 5) logical subscripting, and in general anything that has the term 'logical' in it.

  For the particular line "t = (p<=length(A))", it generates an array which instead of consisting of numbers, are logical quantities either true or false, a special class in matlab. The array p is an array of numerical indices giving the locations of where the various sorted elements came from. For example, if the 4-th element of p is equal to 7 that means that the 7-th element of [A,B] ended up as the 4-th element after sorting. The logical expression p<=length(A) is true whenever a value of p is found that gives the index of an element that must have come from A because it is less than or equal to the length of A, which came first in [A,B]. Those that indicate a source from B will fail this test and be false.

  You should experiment with it on your own and try to verify how it all works by looking at an array before and after sorting and how the index p relates to that. Then you should verify that all the true values in t correspond to elements that were in A and false for all those in B. (Of course use very short A and B for such experiments unless you have lots of time on your hands.)

  (Note: One thing to especially remember about logicals is that when they are subjected to a subsequent arithmetic operation they are converted over to the numbers 1 in place of 'true' and 0 in place of 'false'. This is often of great usefulness.)

  (End of lecture on logicals.)

Roger Stafford

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