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:
obtain numbers when repeateed in every 2 rows

Subject: obtain numbers when repeateed in every 2 rows

From: audley james

Date: 13 Jan, 2011 16:19:05

Message: 1 of 9

G'day friends,

A matrix is given :

X =
      20 30 40 50
      40 50 99 33
      100 88 100 66
      100 66 73 81
 

How can i output the numbers that intersect in 2 consecutive rows?

I am trying to get :

result =
                     40 50
                    100 66

need some help please friends.
Audley

Subject: obtain numbers when repeateed in every 2 rows

From: Paulo Silva

Date: 13 Jan, 2011 17:49:05

Message: 2 of 9

"audley james" <audleyer@gmail.com> wrote in message <ign8lo$4t4$1@fred.mathworks.com>...
> G'day friends,
>
> A matrix is given :
>
> X =
> 20 30 40 50
> 40 50 99 33
> 100 88 100 66
> 100 66 73 81
>
>
> How can i output the numbers that intersect in 2 consecutive rows?
>
> I am trying to get :
>
> result =
> 40 50
> 100 66
>
> need some help please friends.
> Audley

clc;clear
X =[20 30 40 50;
    40 50 99 33;
    100 88 100 66;
    100 66 73 81];
Y=zeros(size(X));
t=size(X);
for a=1:t(1)-1 % run for each line of matrix
    b=X(a,:);
    c=X(a+1,:);
    e=zeros(1,t(2));
    f=e;
    for d=1:t(2)-2 % run for each column of matrix
        e(d)=(b(d)==c(d+2)); %detection \
        f(d)=(b(d+2)==c(d)); %detection /
    end
    Y(a,:)=Y(a,:) | e;
    Y(a+1,:)=Y(a+1,:) | f;
end

disp('numbers that intersect in 2 consecutive rows')
X(find(Y.*X))
disp('To get the same result has asked')
reshape(X(find(Y.*X)),2,2)

Subject: obtain numbers when repeateed in every 2 rows

From: Sean de

Date: 13 Jan, 2011 18:28:04

Message: 3 of 9

"audley james" <audleyer@gmail.com> wrote in message <ign8lo$4t4$1@fred.mathworks.com>...
> G'day friends,
>
> A matrix is given :
>
> X =
> 20 30 40 50
> 40 50 99 33
> 100 88 100 66
> 100 66 73 81
>
>
> How can i output the numbers that intersect in 2 consecutive rows?
>
> I am trying to get :
>
> result =
> 40 50
> 100 66
>
> need some help please friends.
> Audley

How important is the order of the result?
[X2] = sort([X(1:end-1,:),X(2:end,:)],2);
result = unique(X2(diff(X2,1,2)==0))

Subject: obtain numbers when repeateed in every 2 rows

From: Sean de

Date: 13 Jan, 2011 18:42:05

Message: 4 of 9


> How important is the order of the result?
> [X2] = sort([X(1:end-1,:),X(2:end,:)],2);
> result = unique(X2(diff(X2,1,2)==0))

That won't work actually, never mind. It returns a false positive for two of the same number in one line.

Subject: obtain numbers when repeateed in every 2 rows

From: Sean de

Date: 13 Jan, 2011 19:03:05

Message: 5 of 9

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ignh1t$dea$1@fred.mathworks.com>...
>
> > How important is the order of the result?
> > [X2] = sort([X(1:end-1,:),X(2:end,:)],2);
> > result = unique(X2(diff(X2,1,2)==0))
>
> That won't work actually, never mind. It returns a false positive for two of the same number in one line.

This will though:
[X2,idx] = sort([X(1:end-1,:),X(2:end,:)],2);
D = diff(X2,1,2)==0;
[result] = unique(X2(D&idx(:,1:end-1)<=4&idx(:,2:end)>=5))

Subject: obtain numbers when repeateed in every 2 rows

From: audley james

Date: 13 Jan, 2011 19:55:21

Message: 6 of 9

thanks sean.

But I need it unsorted and as seen in the order of the input matrix 'X'

So for example, i if have the following :


      X =[177 249 1731 276
            1731 276 87 55
            219 244 219 249
            219 249 45 12]

The output I want is :

output =
                    1731 276
                    219 249

Asking for some more of your help.

--kurt

Subject: obtain numbers when repeateed in every 2 rows

From: Sean de

Date: 13 Jan, 2011 20:32:15

Message: 7 of 9

"audley james" <audleyer@gmail.com> wrote in message <ignlb9$oov$1@fred.mathworks.com>...
> thanks sean.
>
> But I need it unsorted and as seen in the order of the input matrix 'X'
>
> So for example, i if have the following :
>
>
> X =[177 249 1731 276
> 1731 276 87 55
> 219 244 219 249
> 219 249 45 12]
>
> The output I want is :
>
> output =
> 1731 276
> 219 249
>
> Asking for some more of your help.
>
> --kurt

So there will always be _exactly_ 0 or 2 leftovers from the previous row? I.e. there will be two duplicates (rows1-2,rows3-4) or zero (rows2-3)? Otherwise it will not be possible with a standard rectangular array and you'll need cell arrays.

Subject: obtain numbers when repeateed in every 2 rows

From: audley james

Date: 13 Jan, 2011 21:15:20

Message: 8 of 9

yes there will always be 2.

Subject: obtain numbers when repeateed in every 2 rows

From: Bruno Luong

Date: 13 Jan, 2011 22:42:04

Message: 9 of 9

[m n] = size(X);

X1 = X(1:end-1,:);
X1 = cat(3,X1(:,1:end-1),X1(:,2:end));
X1(:,:,3) = repmat((1:m-1)',[1 n-1]);

X2 = X(2:end,:);
X2 = cat(3,X2(:,1:end-1),X2(:,2:end));
X2(:,:,3) = X1(:,:,3);

X1 = reshape(X1,[],3);
X2 = reshape(X2,[],3);
X1(ismember(X1,X2,'rows'),1:2)

% Bruno

Tags for this Thread

No tags are associated with 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