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:
Cancelling matrix rows

Subject: Cancelling matrix rows

From: Jono

Date: 14 Apr, 2013 21:15:16

Message: 1 of 6

I'm after a way of cancelling "non minimal" rows of a matrix which contain all the non zero terms of a previous row plus more.

For example
a=[ 1, 2, 0;
       3, 4, 5;
       1, 2, 6]

Row 3 of 'a' should be cancelled as a row containing only the values 1 and 2 exists above it.

My current coding uses a for loop to compare each row to every other row using an 'ismember' or all the non zero terms in that row. Any other row that returns a true is member statement is deleted. However the final problem has the potential to have over a million rows meaning this for loop takes an unfeasible amount of time to run.

Does anyone have any ideas on how to speed this process up or take a different approach?

Once sorted into ascending order of number of non zero terms my current coding is:

for R=1:N; %Selects row of matrix
    WF=Phases_Final(R,:);
    TF=WF(WF~=0); %Produces array of non zero terms in row
    
    for X=(R+1):N; %Compares above row to all rows below in the sorted matrix
        if ismember([TF(1,:)],Phases_Final(X,:))==[(ones(1,size(TF,2)))];
            Phases_Final(X,:)=[];
        else
        end
    end
end
.

Any help is appreciated.

Regards,

Jon

Subject: Cancelling matrix rows

From: Yehonatan

Date: 14 Apr, 2013 22:30:09

Message: 2 of 6

Hello Jon,

Give this a try:

clear

%Creating a matrix
matrix = [ 1, 2, 0;
               3, 4, 5;
               1, 2, 6];

%Defining matrix boundries
[matrix_rowmax,matrix_colmax] = size(matrix);

%Creating a new matrix for the rows that follow the rule
newmatrix = 0;

lastrows_maxnumber = -1; % creating a variable that define last rows maximun number
nowrows_minnumber = 0; % creating a variable that defines actual row minimum
                                      % number
followrow_newmatrix = 1; % creating an index variable to follow the rows of the new matrix

for followrow_matrix = 1:matrix_rowmax % a loop that pass on every row of the initial
                                                         % matrix
    nowrows_minnumber = min(matrix(followrow_matrix,:)); % getting the minimum
                                                                                    % cell value of the
                                                                                    % actual row
    
    if(nowrows_minnumber > lastrows_maxnumber) %checking if the minimum number
                                                                        % of the actual row is bigger
                                                                        % from the maximum number
                                                                        % of the last checked rows

        newmatrix(followrow_newmatrix,1:matrix_colmax) =
        matrix(followrow_matrix,1:matrix_colmax); %inserting the row that follow the
                                                                    %rule into the new matrix
        followrow_newmatrix = followrow_newmatrix + 1; %increment the index variable
                                                                               %of the new matrix
        lastrows_maxnumber = max(matrix(followrow_matrix,:)); %defining the new
                                                                                        %maximum number
    end
end

newmatrix

Subject: Cancelling matrix rows

From: Jono

Date: 15 Apr, 2013 11:00:16

Message: 3 of 6

Thanks for the reply, I may have worded the original question wrong but that code isn't quite what I was looking for.

The numbers in the original matrix actually represent and individual 'event' occurring where if all events in a row occur then a system fails. Therefore if any row contains all the events of a previous row plus more then they cannot happen because the system would have already failed due to the earlier row.

It may be easier to describe in letters rather than numbers:

matrix = [ a , b , 0 , 0;
              b , c , d , 0;
              a , b ,c , e;
              b , c, d, e]

Row 3 would be cancelled because if a and b fail then row 1 would have already failed the system meaning d does not have to occur.
Row 4 would be cancelled because if b, c and f fail then row 2 would have already failed the system meaning e does not have to occur.

Hope this explains the problem more clearly. My code does do the task required its just painfully slow as it compares 10^6 rows to 10^6 rows.

Regards,

Jon

Subject: Cancelling matrix rows

From: Yehonatan

Date: 15 Apr, 2013 23:56:07

Message: 4 of 6

Jon,
The only method i came up with involves neural networks. It's a big field and not easy to learn. If you want you can try it.

Subject: Cancelling matrix rows

From: lvn

Date: 16 Apr, 2013 08:05:08

Message: 5 of 6

Not a huge change, but this will be faster as it avoid resizing the matrix in every loop.
Also avoids repetitive calls to size and ones.

toremove=false(N,1);
for R=1:N; %Selects row of matrix
    WF=Phases_Final(R,:);
    TF=WF(WF~=0); %Produces array of non zero terms in row
    
    for X=(R+1):N; %Compares above row to all rows below in the sorted matrix
        toremove(X)=all(ismember(TF,Phases_Final(X,:)));
    end
end
Phases_Final(toremove,:)=[];



"Jono" wrote in message <kkf694$ddd$1@newscl01ah.mathworks.com>...
> I'm after a way of cancelling "non minimal" rows of a matrix which contain all the non zero terms of a previous row plus more.
>
> For example
> a=[ 1, 2, 0;
> 3, 4, 5;
> 1, 2, 6]
>
> Row 3 of 'a' should be cancelled as a row containing only the values 1 and 2 exists above it.
>
> My current coding uses a for loop to compare each row to every other row using an 'ismember' or all the non zero terms in that row. Any other row that returns a true is member statement is deleted. However the final problem has the potential to have over a million rows meaning this for loop takes an unfeasible amount of time to run.
>
> Does anyone have any ideas on how to speed this process up or take a different approach?
>
> Once sorted into ascending order of number of non zero terms my current coding is:
>
> for R=1:N; %Selects row of matrix
> WF=Phases_Final(R,:);
> TF=WF(WF~=0); %Produces array of non zero terms in row
>
> for X=(R+1):N; %Compares above row to all rows below in the sorted matrix
> if ismember([TF(1,:)],Phases_Final(X,:))==[(ones(1,size(TF,2)))];
> Phases_Final(X,:)=[];
> else
> end
> end
> end
> .
>
> Any help is appreciated.
>
> Regards,
>
> Jon

Subject: Cancelling matrix rows

From: Jono

Date: 22 Apr, 2013 10:35:09

Message: 6 of 6

"lvn" wrote in message <kkj0nk$nc8$1@newscl01ah.mathworks.com>...
> Not a huge change, but this will be faster as it avoid resizing the matrix in every loop.
> Also avoids repetitive calls to size and ones.
>
> toremove=false(N,1);
> for R=1:N; %Selects row of matrix
> WF=Phases_Final(R,:);
> TF=WF(WF~=0); %Produces array of non zero terms in row
>
> for X=(R+1):N; %Compares above row to all rows below in the sorted matrix
> toremove(X)=all(ismember(TF,Phases_Final(X,:)));
> end
> end
> Phases_Final(toremove,:)=[];
>
>
>
> "Jono" wrote in message <kkf694$ddd$1@newscl01ah.mathworks.com>...
> > I'm after a way of cancelling "non minimal" rows of a matrix which contain all the non zero terms of a previous row plus more.
> >
> > For example
> > a=[ 1, 2, 0;
> > 3, 4, 5;
> > 1, 2, 6]
> >
> > Row 3 of 'a' should be cancelled as a row containing only the values 1 and 2 exists above it.
> >
> > My current coding uses a for loop to compare each row to every other row using an 'ismember' or all the non zero terms in that row. Any other row that returns a true is member statement is deleted. However the final problem has the potential to have over a million rows meaning this for loop takes an unfeasible amount of time to run.
> >
> > Does anyone have any ideas on how to speed this process up or take a different approach?
> >
> > Once sorted into ascending order of number of non zero terms my current coding is:
> >
> > for R=1:N; %Selects row of matrix
> > WF=Phases_Final(R,:);
> > TF=WF(WF~=0); %Produces array of non zero terms in row
> >
> > for X=(R+1):N; %Compares above row to all rows below in the sorted matrix
> > if ismember([TF(1,:)],Phases_Final(X,:))==[(ones(1,size(TF,2)))];
> > Phases_Final(X,:)=[];
> > else
> > end
> > end
> > end
> > .
> >
> > Any help is appreciated.
> >
> > Regards,
> >
> > Jon

Thanks for the help. Whilst this has sped the code up slightly it's still unfeasible for some of the larger problems I have to tackle. One is stretching into 6 million different system failure combinations...... Comparing every row to every row is just too much!

Regards,

Jon

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