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:
preserve order of matrix

Subject: preserve order of matrix

From: ravi

Date: 11 Dec, 2010 19:05:05

Message: 1 of 8

Hello folks,

I have a matrix as follows:

A =
         10 20 30 40
         30 40 50 60
         55 33 12 24
         19 49 55 33

I want to return the values that intersect in 2 consecutive rows,so to get the answer:

answer =
                      30 40
                      55 33

I did it in my code below but my problem is that I need to preserve the order, instead my code sorts the rows in ascending numerical order, so if you run my code the answer would be:

wrong_answer =
                              30 40
                              33 55

Can some help me with this please?

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 A = [ 10 20 30 40
         30 40 50 60
         55 33 12 24
         19 49 55 33];

rowsPerIter =2;
      
      for i = 1:(size(A,1)/2)
       
       from=(i-1)*rowsPerIter+1;
    to = from+rowsPerIter-1;

    corner{i} = intersect(A(from,:),A(to,:));
     
      end
      corners = cell2mat(corner.')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        

Subject: preserve order of matrix

From: dpb

Date: 11 Dec, 2010 19:47:35

Message: 2 of 8

ravi wrote:
> Hello folks,
>
> I have a matrix as follows:
>
> A = 10 20 30 40
           30 40 50 60
> 55 33 12 24
> 19 49 55 33
>
> I want to return the values that intersect in 2 consecutive rows,so to
> get the answer:
>
> answer = 30 40
> 55 33
>
> I did it in my code below but my problem is that I need to preserve the
> order, ...

Perhaps within the consecutive rows the response I made earlier this AM
to Aidy in a thread "Extract numbers..." might have some usage regarding
how to do somesuch. His case had a subset corresponding array, but
yours could generate such an array by comparison. I've got another
commitment in a few minutes so no time to consider at the moment, sorry...

--

Subject: preserve order of matrix

From: ravi

Date: 11 Dec, 2010 21:57:04

Message: 3 of 8

I looked at the post you suggested, but still not sure how it can help.....

Subject: preserve order of matrix

From: Oleg Komarov

Date: 12 Dec, 2010 01:48:04

Message: 4 of 8

"ravi " <ravi_071@hotmail.com> wrote in message <ie0s3g$ces$1@fred.mathworks.com>...
> I looked at the post you suggested, but still not sure how it can help.....
A = [ 10 20 30 40
         30 40 50 60
         55 33 12 24
         19 49 55 33];

oddRows = 1:2:size(A,1)-1;
% Use ismember to locate the elements and retrieve them from odd rows
Out = arrayfun(@(x) A(x,ismember(A(x,:),A(x+1,:))),oddRows.','un',0);
% Concatenate results into a double matrix
Out = cat(1,Out{:});

Oleg

Subject: preserve order of matrix

From: ravi

Date: 12 Dec, 2010 04:08:04

Message: 5 of 8

ah thanks a lot oleg...

best ,
ravi

Subject: preserve order of matrix

From: ravi

Date: 12 Dec, 2010 10:47:04

Message: 6 of 8

Oleg,

I have a new problem.

My input matrix changes as follows:

   A = [ 30 40 30 40
         30 40 50 60
         55 33 12 24
         19 49 55 33];

As you could see , in the first row the numbers are repeated twice.

As a result in the code line :

Out = arrayfun(@(x) A(x,ismember(A(x,:),A(x+1,:))),oddRows.','un',0)

'Out' gives a 1x4 output , and a 1x2 output, hence,I get the error "CAT arguments dimensions are not consistent".

Can I please ask for some more help on adjusting this problem?

thanks a lot
ravi

Subject: preserve order of matrix

From: ravi

Date: 12 Dec, 2010 12:30:05

Message: 7 of 8

Hi,

With matrix A as follows:

   A = [ 70 40 70 40
         70 40 50 60
         55 33 12 24
         19 49 55 33 ];

I'm trying to obtain:

result =
             70 40
             55 33

However, I'm not to familiar with "arrayfun" and in my code below I'm getting 'Index dimension error'. Please help.

%%%%%%%%%%% CODE %%%%%%%%%%%%%%%%
clc;clear all;
     A = [ 70 40 70 40
         70 40 50 60
         55 33 12 24
         19 49 55 33 ];
     
oddRows = 1:2:size(A,1)-1;
% Use ismember to locate the elements and retrieve them from odd rows
Out = arrayfun(@(x) A(x,ismember(A(x,:),A(x+1,:))),oddRows.','un',0);

for i = 1:size(Out,1)
   
   M = cell2mat(Out(i,:));
   if numel(M) > 2
       
       result{i} = arrayfun(@(x) M(x,ismember(M(:,x:x+1),M(:,x+2:x+3))),oddRows.','un',0);

   else
       result{i} = M;
   end
   
end
result = result(:)

Subject: preserve order of matrix

From: Oleg Komarov

Date: 12 Dec, 2010 16:14:04

Message: 8 of 8

How do you get matrix A and why do you need an ordered result?

If you try to explain the rationale behind it we may find another way of doing it.

Otherways you have to implement a unique call and sort it back.

Cellfun, arrayfun and structfun are functions that imitate the for construct.
The first argument is basically the operations you apply inside the loop, the second to nth argument are the variables you use in the loop and at last you specify some additional options like collecting output in a cell ['un',0]

Out = arrayfun(@(x) A(x,:), 1:10,'un',0)

equivalently:

for x = 1:10
Out = {A(x,:)};
end


Oleg

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