I have data organized in a Matrix from which I want to select a sample based on a certain (simple) criterion, e.g. no cell in the first column to be negative.

I was quickly able to find a way to do it, inspired by a method Preben Alsholm used in a recent mapleprimes post.

But, I wondered, is that the most natural approach? So I quickly found another approach and compared them.

Any other suggestions welcome. Particularly methods that could be used when more complicated criteria are applied.

Two comments,

  1. The statement seq(`if`(M[i,1]<0,M[i,1..-1],NULL),i= ... does just the opposite of what I expect, i.e. it keeps the offending rows. Thus I need to reverse the inequality seq(`if`(M[i,1]>0,M[i,1..-1],NULL),i= ... to obtain the desired result. Something lost to my intuition here.
  2. I am reporting times and bytes used and allocated after a full exit/restart of Maple.


M := Matrix(1..100,1..3,rand(-100..100)):

MatrixSelectRows1 := proc(M::Matrix)
  local i ;
end proc:

st,ba,bu := time(),kernelopts(bytesalloc),kernelopts(bytesused):
M1 := MatrixSelectRows1(M):

MatrixSelectRows2 := proc(M::Matrix)
  local i, U ;
  U := NULL :
  for i to LinearAlgebra:-RowDimension(M) do
    if M[i,1] < 0
      then U := U,i ;
    end if ;
  end do ;
end proc:

st,ba,bu := time(),kernelopts(bytesalloc),kernelopts(bytesused):
M2 := MatrixSelectRows2(M):

Please Wait...