MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Given a matrix, find position of first non-zero element in each


Hi,

> This is how I solved this problem and after a bit of  struggle. I wanted to 
> see if I could avoid using a Table, and solve it just using Patterns and 
> Position and Select, but could not so far.
> 
> 
> Table[Flatten[{i, Flatten[Position[A[[i,All]], _?(#1 != 0 & ), 1, 1]]}], {i, 
> 1, 6}]
> 
> Out[174]= {{1, 3}, {2, 1}, {3, 2}, {4, 1}, {5, 2}, {6, 2}}
> 
> I am not happy with the above solution. I am sure there is a better one (the 
> above also do not work well when one row has all zeros).
> 
> Do you see a better and more elegant way to do this?

I absolutley don't think that it is a value per se to avoid Table, but
here is one solution without Table:

MapIndexed[Flatten[{#2, Position[#1, _?(# != 0 &), 1, 1]}] &,A]

hth,

albert


  • Prev by Date: Re: solve an integration equation
  • Next by Date: BinaryReadList
  • Previous by thread: Re: Message passing in mathematica?
  • Next by thread: Re: Given a matrix, find position of first non-zero element in each