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:
Logical Sorting

Subject: Logical Sorting

From: Michael

Date: 31 Jan, 2009 01:16:02

Message: 1 of 5

I was wondering if anyone could point me in the right direction for a problem I have. Lets say I have two continuous functions, y1 and z1, they could be anything but I am going to use sin and sinc.:

x = linspace(0,6*pi,101);
y1 = sin(x);
z1 = sin(x)./x;
figure(1)
plot(x,y1,'-x',x,z1,'-x')

and somehow they get randomly scrambled:

for ii = 1:101
   ind = round(rand(1))+1;
   if ind == 1
        y2(ii) = y1(ii);
        z2(ii) = z1(ii);
   else
       y2(ii) = z1(ii);
       z2(ii) = y1(ii);
   end
end
figure(2)
plot(x,y2,'x',x,z2,'x')

I'm hoping someone can point me in the right direction on how y1 and z1 can be reassembled into their own arrays once more; assuming I have no information about how they were randomly scrambled. Or looking at the figures, I want to be able to go from figure(2) to figure(1) once more.

Is there a term for this type of sorting in computer science so that I may research it (other then subject title)?

I am mainly concerned with regions where the two functions cross.

All I know of my data is that:
1. The data is continuous
2. The data is not a specific "common function."
3. I have some control over how often the data is sampled, but the sparser the data can be the better.

Thank You

Subject: Logical Sorting

From: Roger Stafford

Date: 2 Feb, 2009 02:00:05

Message: 2 of 5

"Michael" <mlcooper777@aol.com> wrote in message <gm08oh$17i$1@fred.mathworks.com>...
> I was wondering if anyone could point me in the right direction for a problem I have. Lets say I have two continuous functions, y1 and z1, they could be anything but I am going to use sin and sinc.:
>
> x = linspace(0,6*pi,101);
> y1 = sin(x);
> z1 = sin(x)./x;
> figure(1)
> plot(x,y1,'-x',x,z1,'-x')
>
> and somehow they get randomly scrambled:
> .......

  What you describe is certainly mysterious, Michael. If I were in your position I would try making the linespec marker different for the two functions being plotted to make sure of the identity of the points being plotted. Both your plots are using the same '-x' in the code you show. Also I would try eliminating the line style feature and just concentrate on the points themselves. Just use 'x', 'o', '+', '*', etc.

  If you can see some pattern as to which points get reversed, that would be extremely useful. Do you get precisely the same results when you repeat the experiment exactly? The odds are they aren't actually random.

  Also the z1 = sin(x)./x line should result in a NaN for x = 0 from the zero divided by zero occurrence there. Matlab's division operation has no way of knowing that sin(x)/x approaches 1 as a limit as x approaches zero and it should complain vociferously. Did it do so and if not why not? Try either shortening the range to avoid x = 0 or use some other function, like cos(x).

  The secret in trouble-shooting anomalies like this is to reduce the situation to its purest and simplest form in such a manner as to isolate the trouble-making aspects as much as possible. When you make a change that makes your trouble go away, retreat from that change and make other simplification changes one at a time, checking the behavior after each one. Boil it down to its essence. If nothing else that allows you describe the situation to support people more easily and convincingly.

Roger Stafford

Subject: Logical Sorting

From: Matt Fig

Date: 2 Feb, 2009 02:37:02

Message: 3 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message
>
> What you describe is certainly mysterious, Michael. If I were in your position I would try making the linespec marker different for the two functions being plotted to make sure of the identity of the points being plotted. Both your plots are using the same '-x' in the code you show. Also I would try eliminating the line style feature and just concentrate on the points themselves. Just use 'x', 'o', '+', '*', etc.
>
> If you can see some pattern as to which points get reversed, that would be extremely useful. Do you get precisely the same results when you repeat the experiment exactly? The odds are they aren't actually random.


I took the problem a little differently than it sounds like Roger did. As I understand it, you have two unknown continuous functions, the data of which got scrambled in some unknown way, not just in each data set separately, but between the sets. The task is to figure out, either through array manipulations or by plotting, how to unscramble them.
Thus plotting the arrays with different linespec's wouldn't help because the data itself is scrambled.
Seems like a tough problem in general. Is the spacing of the independent variable the same for both functions? If so it seems like you could go point by point and select the closest point from the last (assuming the functions are "well-behaved" and there is some significant spacing between them in the dependent variable.)




9{2!sk'"_(ws"(ssz#2t!2t~u{2y,W~)"2'@} sR 2zL#w92zs"wu"'2"ws

Subject: Logical Sorting

From: ImageAnalyst

Date: 2 Feb, 2009 03:40:23

Message: 4 of 5

Michael:
This reminds me of "The Cocktail Party Problem" which means that you
have a listener at a cocktail party and he/she hears one signal, which
is the sum of all the different voices. Yet the listener is able to
unscramble this cacophony to pick out (reconstruct or extract) one
particular voice. It's been solved with Independent Components
Analysis (ICA). You can check out these web sites for further
information:
http://www.cis.hut.fi/projects/ica/icademo/
http://www.cnl.salk.edu/~tony/ica.html
For your situation, it seems that, as contrary as it seems, you might
be best off adding your two scrambled signals together and then
applying ICA.
Let me know if it works out for you.

Matt's idea also seems like it should work but I think you'll have to
check the slope as well as the values because otherwise, as you're
restoring the two signals, when the two curves cross, you might end up
chasing along the wrong one.
Regards,
ImageAnalyst

Subject: Logical Sorting

From: Roger Stafford

Date: 2 Feb, 2009 06:22:01

Message: 5 of 5

"Matt Fig" <spamanon@yahoo.com> wrote in message <gm5m8e$qpk$1@fred.mathworks.com>...
> I took the problem a little differently than it sounds like Roger did. ......

  Thanks, Matt and ImageAnalyst, for setting me straight on this question. I am a bit chagrined that I hadn't read it carefully enough and totally misunderstood its meaning (and in the process wasted a lot of time.) :-(

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