Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Repeated numbers in Matrix

1 view
Skip to first unread message

John

unread,
Feb 21, 2012, 1:37:12 PM2/21/12
to
Hi,

I'm working on a problem where I need to get a+b+c=0

With the M = Matrix
for i = 1:(length(M)-2
a= M(i)
for j = i+1:(length(M)-1
b= M(j)
for k = j+1:(length(M))
c= M(k)
if a+b+c==0
x(i,:) = [a b c]


What I need is to have it from not popping out repeated numbers in each of the rows
for example, 2 -2 0 is the same as -2 2 0.

what would be a good way to get it to not do this?

Matt J

unread,
Feb 21, 2012, 1:51:11 PM2/21/12
to
"John " <drkan...@yahoo.com> wrote in message <ji0o8n$76b$1...@newscl01ah.mathworks.com>...
> Hi,
>
> I'm working on a problem where I need to get a+b+c=0
>
> With the M = Matrix
> for i = 1:(length(M)-2
> a= M(i)
> for j = i+1:(length(M)-1
> b= M(j)
> for k = j+1:(length(M))
> c= M(k)
> if a+b+c==0
> x(i,:) = [a b c]
>
>
> What I need is to have it from not popping out repeated numbers in each of the rows
> for example, 2 -2 0 is the same as -2 2 0.
===============

Are these triples equivalent because order is not important, or because sign is not important? If the former

unique(sort(x,2),'rows');

John

unread,
Feb 21, 2012, 2:10:14 PM2/21/12
to

> Are these triples equivalent because order is not important, or because sign is not important? If the former
>
> unique(sort(x,2),'rows');

====================

Also forgot to say, but the matrix M pops out different values every time because it uses "randi", so the results for the x(i,:) are can sometimes have repeats and sometimes not have any repeats.

Does this affect the use of unique(sort(x,2),'rows'); ?

John

unread,
Feb 21, 2012, 2:27:11 PM2/21/12
to
Tried putting in unique(sort(x,2),'rows');

It only seems to sort the numbers

Ex for one of the one's I tried

2 2 -4
3 -4 1
-3 2 1 %
-3 2 1 %

% are repeats which I'm trying to get rid of

Matt J

unread,
Feb 21, 2012, 2:59:11 PM2/21/12
to
"John " <drkan...@yahoo.com> wrote in message <ji0r6f$hdf$1...@newscl01ah.mathworks.com>...
================

Not sure what the problem is, but I'm guessing there is code you're not showing. When I plug in the data you have above, the duplicates are gone, as expected:



>> unique(sort(x,2),'rows')

ans =

-4 1 3
-4 2 2
-3 1 2

John

unread,
Feb 21, 2012, 5:46:11 PM2/21/12
to
I'm trying to eliminate numerically equivalent values so that it doesn't have any repeated solutions.

This should be able to match solutions which contain the same values but in different orders.

Ex.
[3 1 -4] and [3 -4 1] are solutions, but the method should still identify them as equivalent and only give one of them.

Matt J

unread,
Feb 21, 2012, 6:23:09 PM2/21/12
to
"John " <drkan...@yahoo.com> wrote in message <ji16ri$sd1$1...@newscl01ah.mathworks.com>...
=================

Once again, though, I think the code I gave you does that. It worked for me on the sample data set that you gave.

John

unread,
Feb 21, 2012, 7:10:11 PM2/21/12
to
It works for some of the values of M, but I need it to work for every one of them

For the random values M = 5 0 -4 4 2 -1 5 -2

I get

5 -4 -1 %
0 2 -2
-4 -1 5 %

% these 2 are repeated

Ex.
M = 2 -1 -3 -4 0 -3 3 -1

ans =

2 -1 -1
0 0 0 %
-3 0 3 %
0 0 0 %
0 -3 3 %

%Other repeats

Matt J

unread,
Feb 22, 2012, 11:12:13 AM2/22/12
to
"John " <drkan...@yahoo.com> wrote in message <ji1bp3$dfu$1...@newscl01ah.mathworks.com>...
======================

Please execute ONLY the following 2 consecutive lines and tell me what the output is,

x=[ 5 -4 -1
0 2 -2
-4 -1 5] ;

Output=unique(sort(x,2),'rows')

John

unread,
Feb 22, 2012, 11:59:14 AM2/22/12
to
Oh wow now I feel stupid been trying to define a new variable instead of use the same output value for it.


Now I just need it to show x=0 when the matrix has no values

John

unread,
Feb 22, 2012, 12:04:13 PM2/22/12
to
the x =0 might be also why I'm getting and error for some of the random numbers


Undefined function or variable "x".

Error in test (line 24)
x = unique(sort(x,2),'rows');
0 new messages