Re: TIB: searching matrixes
[Prev][Next][Index][Thread]
Re: TIB: searching matrixes
>Subject: TIB: searching matrixes
>
>is there a way to search a matrix to find a number in any row or column
>without having to look at each individual row and column
>eg
>[[1 2]
> [3 4]]
>if [a](1,1) = 2
>then
>whatever
>if [a](1,2) = 2
>then
>whatever
>if [a](2,1) = 2
>then
>whatever
>if [a](2,2) = 2
>then
>whatever
>instead of this something that does searches the matrix in one command
Try this function for the 89, 92 and 92+:
matser(m,n)
Func
Local b
colDim(m)->b
string(mat->list(abs(m .- n)))->m
max(inString(m,"{0"),inString(m,",0"))->n
If n=0
Return {}
dim(expr(left(m,n+1)&"}"))-1->m
Return {int(m/b),mod(m,b)}+1
EndFunc
The syntax is matser(the matrix you want to search, the number to find)
The result is {} if no match is found otherwise {column, row}
The stipulation is it only finds the first occurrence of the number in a
matrix, thought the function can be easily modified to search for all
occurrences or a calling program can make repeated calls to the function
modify the returned entry each time. Also this program can be modified to
search strings as well with minimal modification.
This function is more efficient than a linear search in most cases. A few
stats:
Timing (just by looking as the seconds pass on a watch)
23x23 matrix filled with zeros location 23,23 contains 50
matser 5 seconds
linear search 35 seconds
23x23 matrix filled with zeros location 12,12 contains 50
matser 5 seconds
linear search 11 seconds
23x23 matrix filled with zeros location 1,1 contains 50
matser 4 seconds
linear search 1 second
The matser search to be preferable to the linear search. I would assume
this is the fact that TI-BASIC is slower than most built-in functions.
I'll leave optimization to others.
If you are interested in speed/space statistics and optimization
techniques on TI-BASIC visit my web site:
http://www.iserv.net/~mikev/index.html
I hope this is helpful,
Michael Van Der Kolk
mikev@iserv.net
http://www.iserv.net/~mikev