
Find the median value of an array.
Hello,
Given an array with 10,000 real values, I want to find the median. And
I want to do this about 100,000 times. My current plan is to implement
Quicksort or Merge Sort in Fortran and pick the middle point of the
array to get the median. I was just wondering Fortran already has a
way to do this. I am sure I am not the first person in history to want
the median of a group of values in Fortran. But I cannot find an
intrinsic function called "median".
Any thoughts?
Thanks.
Daniel.


0




Reply

Daniel

11/11/2010 4:49:36 PM 

See related articles to this posting
"Daniel Carrera" <dcarrera@gmail.com> wrote in message
news:d510fdac68cd4637811d985d58ec0541@40g2000vbn.googlegroups.com...
> Given an array with 10,000 real values, I want to find the median. And
> I want to do this about 100,000 times. My current plan is to implement
> Quicksort or Merge Sort in Fortran and pick the middle point of the
> array to get the median. I was just wondering Fortran already has a
> way to do this. I am sure I am not the first person in history to want
> the median of a group of values in Fortran. But I cannot find an
> intrinsic function called "median".
I wrote up some stuff a while back:
http://home.comcast.net/~kmbtib/Fortran_stuff/order_stat.i90
http://home.comcast.net/~kmbtib/Fortran_stuff/order_stat_test.f90
Can't recall whether it's exactly what you need, but it's faster
than sorting, anyhow. Write back if you encounter problems.

write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D85, &
6.0134700243160014d154/),(/'x'/)); end


0




Reply

James

11/11/2010 4:56:53 PM


In article <ibh78q$l28$1@news.eternalseptember.org>,
James Van Buskirk <not_valid@comcast.net> wrote:
>"Daniel Carrera" <dcarrera@gmail.com> wrote in message
>news:d510fdac68cd4637811d985d58ec0541@40g2000vbn.googlegroups.com...
>
>> Given an array with 10,000 real values, I want to find the median. And
>> I want to do this about 100,000 times. My current plan is to implement
>> Quicksort or Merge Sort in Fortran and pick the middle point of the
>> array to get the median. I was just wondering Fortran already has a
>> way to do this. I am sure I am not the first person in history to want
>> the median of a group of values in Fortran. But I cannot find an
>> intrinsic function called "median".
>
>I wrote up some stuff a while back:
>
>http://home.comcast.net/~kmbtib/Fortran_stuff/order_stat.i90
>http://home.comcast.net/~kmbtib/Fortran_stuff/order_stat_test.f90
>
>Can't recall whether it's exactly what you need, but it's faster
>than sorting, anyhow. Write back if you encounter problems.
There is a much faster algorithm, incidentally.
1) Estimate a range around its value using a small, fixed sample.
2) Pass through the array, copying the values within the range,
and counting those to the left and right.
3) Repeat on the subset if the counts were good, and using the
fact that you now know which side it was if not.
4) Repeat until the sample is small enough to sort.
You can use a limited size array for the subset, incidentally,
and do a modification of the above if you run out of space.
The theory of this is standard statistics.
I have the code somewhere, but wouldn't be able to find it,
because I can't even rememember which decade I did it in and
scanning through all my archives (in half a dozen formats) is
too timeconsuming. Sorry.
Regards,
Nick Maclaren.


0




Reply

nmm1

11/11/2010 5:40:48 PM


nmm1@cam.ac.uk wrote:
(snip)
>>"Daniel Carrera" <dcarrera@gmail.com> wrote in message
>>news:d510fdac68cd4637811d985d58ec0541@40g2000vbn.googlegroups.com...
>>> Given an array with 10,000 real values, I want to find the median.
(snip)
> There is a much faster algorithm, incidentally.
Is that the one in Knuth, "The Art of Computer Programming", my
guess is volume 3? It doesn't sound familiar, but I could have
forgotten how it went.
> 1) Estimate a range around its value using a small, fixed sample.
> 2) Pass through the array, copying the values within the range,
> and counting those to the left and right.
> 3) Repeat on the subset if the counts were good, and using the
> fact that you now know which side it was if not.
> 4) Repeat until the sample is small enough to sort.
> You can use a limited size array for the subset, incidentally,
> and do a modification of the above if you run out of space.
> The theory of this is standard statistics.
> I have the code somewhere, but wouldn't be able to find it,
> because I can't even rememember which decade I did it in and
> scanning through all my archives (in half a dozen formats) is
> too timeconsuming. Sorry.
 glen


0




Reply

glen

11/11/2010 9:17:12 PM


In article <ibhmgo$b1q$1@news.eternalseptember.org>,
glen herrmannsfeldt <gah@ugcs.caltech.edu> wrote:
>
>>>> Given an array with 10,000 real values, I want to find the median.
>
>> There is a much faster algorithm, incidentally.
>
>Is that the one in Knuth, "The Art of Computer Programming", my
>guess is volume 3? It doesn't sound familiar, but I could have
>forgotten how it went.
No. Knuth is not strong on statistical algorithms. It includes
the one that James Van Buskirk posted.
Regards,
Nick Maclaren.


0




Reply

nmm1

11/11/2010 9:28:11 PM


nmm1@cam.ac.uk wrote:
> In article <ibhmgo$b1q$1@news.eternalseptember.org>,
> glen herrmannsfeldt <gah@ugcs.caltech.edu> wrote:
>>>>> Given an array with 10,000 real values, I want to find the median.
>>> There is a much faster algorithm, incidentally.
>>Is that the one in Knuth, "The Art of Computer Programming", my
>>guess is volume 3? It doesn't sound familiar, but I could have
>>forgotten how it went.
> No. Knuth is not strong on statistical algorithms.
The one in Knuth is supposed to be O(N), which doesn't mean
that it is fastest for any, or even most, sets of input data.
Some of this seems to be in the wikipedia page Selection_algorithm.
The wikipedia page for Median_graph has a reference to Knuth's
volume 4, I believe available on the web, but not in print.
> It includes the one that James Van Buskirk posted.
I don't always read posted links.
 glen


0




Reply

glen

11/11/2010 9:43:13 PM


In article <ibho1g$b1q$3@news.eternalseptember.org>,
glen herrmannsfeldt <gah@ugcs.caltech.edu> wrote:
>
>>>>>> Given an array with 10,000 real values, I want to find the median.
>
>>>> There is a much faster algorithm, incidentally.
>
>>>Is that the one in Knuth, "The Art of Computer Programming", my
>>>guess is volume 3? It doesn't sound familiar, but I could have
>>>forgotten how it went.
>
>> No. Knuth is not strong on statistical algorithms.
>
>The one in Knuth is supposed to be O(N), which doesn't mean
>that it is fastest for any, or even most, sets of input data.
The one I posted is also O(N) with probability one, but with a
much lower factor. It is very common that replacing a pure
mathematical worst case with a probability zero one produces
a much faster algorithm, that is every bit as reliable in real
use.
Regards,
Nick Maclaren.


0




Reply

nmm1

11/11/2010 9:47:00 PM


Thanks for the help.
Now I know that I need to write my own function. Thanks for the
suggestions for algorithms. I'll try to pick something that I
understand, but is efficient. I have an idea for an algorithm based on
the quicksort / mergesort idea. Probably not as efficient as James'
code, but it should be good enough, and it'll be something I
understand.
Thanks for the help.
Cheers,
Daniel.


0




Reply

Daniel

11/12/2010 9:52:08 AM


On 12.11.2010. 10:52, Daniel Carrera wrote:
> Thanks for the help.
>
> Now I know that I need to write my own function. Thanks for the
> suggestions for algorithms. I'll try to pick something that I
> understand, but is efficient. I have an idea for an algorithm based on
> the quicksort / mergesort idea. Probably not as efficient as James'
> code, but it should be good enough, and it'll be something I
> understand.
>
> Thanks for the help.
Take a look at Michel Olagnon's (who used to frequent clf):
http://www.fortran2000.com/rank/
In particular, INDMED and VALMED are based on Knuth. I used some
of other Michel's routines, but not these ones.

Jugoslav
www.xeffort.com


0




Reply

Jugoslav

11/12/2010 3:02:51 PM


On Nov 12, 4:02=A0pm, Jugoslav Dujic <jdu...@yahoo.com> wrote:
> Take a look at Michel Olagnon's (who used to frequent clf):
>
> http://www.fortran2000.com/rank/
>
> In particular, INDMED and VALMED are based on Knuth. I used some
> of other Michel's routines, but not these ones.
Thanks. I'll take a look at VALMED. In fact, I have a question
already. VALMED starts with:
Recursive Function D_valmed (XDONT) Result (res_med)
I thought that function definitions always needed a return type like
"real function foo()". When I compile my own code with gfortran it
always complains if I don't include a return type, but Michel's module
compiles just fine the way it is. What's Michel doing that allows him
to get away with a function without a return type?
Anyway, I'll try to understand how Michel's code works. I did come up
with a nice, simple algorithm to get the median in O(n log n), and is
fast enough for my purposes (will add ~2.7 mins to a program that runs
8 hours). But I'll probably learn something interesting by looking at
Michel's code.
For reference, my code is below. It sorts an array of real numbers and
is inspired quicksort / mergesort:
real function qmedian(list)
real, dimension(:), intent(in) :: list
integer :: n
real :: dummy
n =3D size(list)
dummy =3D rand(1)
if (mod(n,2) =3D=3D 0) then
qmedian =3D qmedian_helper(list, n/2)
else
qmedian =3D qmedian_helper(list, n/2 + 1)
end if
end function
real recursive function qmedian_helper(list, q) result(res)
! Parameters
real, dimension(:), intent(in) :: list
integer, intent(in) :: q
! Internal variables
real, allocatable, dimension(:) :: left, right
real :: pivot, lmax, lmin
integer :: n, j
! Algorithm
n =3D size(list)
call bounds(list, lmin, lmax)
if (q =3D=3D 1) then
res =3D lmin
else if (q =3D=3D n) then
res =3D lmax
else
!!! Split 'list' into two parts and recurse.
! Pick a random element from the list.
pivot =3D list(int(rand(0)*n)+1)
j =3D count(list <=3D pivot)
! The previous conditions should remove corner cases.
allocate(left(j))
allocate(right(nj))
left =3D pack(list, list <=3D pivot)
right=3D pack(list, list > pivot)
if (j >=3D q) then
! Left side contains kth element.
res =3D qmedian_helper(left, q)
else
! Right side contains kth element.
res =3D qmedian_helper(right, qj)
end if
end if
end function
subroutine bounds(list, lmin, lmax)
real, dimension(:), intent(in) :: list
real, intent(out) :: lmin, lmax
integer :: n, i
n =3D size(list)
lmin =3D list(1)
lmax =3D list(1)
do i =3D 2,n
if (list(i) < lmin) lmin =3D list(i)
if (list(i) > lmax) lmax =3D list(i)
end do
end subroutine
Cheers,
Daniel.


0




Reply

Daniel

11/12/2010 7:47:41 PM


Daniel Carrera wrote:
> On Nov 12, 4:02 pm, Jugoslav Dujic <jdu...@yahoo.com> wrote:
>> Take a look at Michel Olagnon's (who used to frequent clf):
>>
>> http://www.fortran2000.com/rank/
>>
>> In particular, INDMED and VALMED are based on Knuth. I used some
>> of other Michel's routines, but not these ones.
>
> Thanks. I'll take a look at VALMED. In fact, I have a question
> already. VALMED starts with:
>
> Recursive Function D_valmed (XDONT) Result (res_med)
>
> I thought that function definitions always needed a return type like
> "real function foo()".
I suspect the answer is made clear if you look at the first point under
"programming style" in the above link,
Ian


0




Reply

Ian

11/12/2010 7:57:41 PM


On Nov 12, 11:47=A0am, Daniel Carrera <dcarr...@gmail.com> wrote:
> On Nov 12, 4:02=A0pm, Jugoslav Dujic <jdu...@yahoo.com> wrote:
>
> > Take a look at Michel Olagnon's (who used to frequent clf):
>
> >http://www.fortran2000.com/rank/
>
> > In particular, INDMED and VALMED are based on Knuth. I used some
> > of other Michel's routines, but not these ones.
>
> Thanks. I'll take a look at VALMED. In fact, I have a question
> already. VALMED starts with:
>
> Recursive Function D_valmed (XDONT) Result (res_med)
>
> I thought that function definitions always needed a return type like
> "real function foo()". When I compile my own code with gfortran it
> always complains if I don't include a return type, but Michel's module
> compiles just fine the way it is. What's Michel doing that allows him
> to get away with a function without a return type?
Removing the large comment from Michel's code, I finds
the first 3 lines:
Recursive Function D_valmed (XDONT) Result (res_med)
Real (kind=3Dkdp), Dimension (:), Intent (In) :: XDONT
Real (kind=3Dkdp) :: res_med
The return type is declared in line 3.

steve


0




Reply

steve

11/12/2010 7:59:39 PM


Daniel Carrera <dcarrera@gmail.com> wrote:
> Recursive Function D_valmed (XDONT) Result (res_med)
>
> I thought that function definitions always needed a return type like
> "real function foo()". When I compile my own code with gfortran it
> always complains if I don't include a return type, but Michel's module
> compiles just fine the way it is. What's Michel doing that allows him
> to get away with a function without a return type?
He presumably declares the type later in the code in a separate
statement. Not only is that allowed, it is what I prefer and recommend.
There can be problems with declaring the type directly on the function
statement; it works in most cases, but not all of them. Declaring the
type in a separate type declaration statement works in *ALL* cases. I
prefer to do things that work all the time so that I don't need to worry
about checking when I have crossed the boundary between the cases that
work and the ones that don't.
Also note that when you declare the result variable to have a separate
name, it is the result variable whose type gets declared  not the
function.
As an aside, no, you don't have to explicitly declare the type. If you
don't declare the type, implicit typing applies, just as with any other
variable. I disrecommend the use of implicit typing, and if you use
impkicit none, the compiler will bitch about it, but it is allowed. If
your compiler complains when you omit the type, then probably you are
using implicit none (which I do recommend). Either that or you are doing
something that is incompatible with the implicit type. Be aware that
implicit typing does *NOT* mean that an entity has no type; it just
means that the type is determined by the implicit typing rules instead
of by an explicit type declaration. I wouldn't mention this except that
I wonder why you would ever be trying to write code that doesn't declare
the return type and also isn't deliberately using implicit typing; the
only reason I can think of (other than just an accident) is that one
might think that failing to explicitly declare the type somehow left it
generic.

Richard Maine  Good judgment comes from experience;
email: last name at domain . net  experience comes from bad judgment.
domain: summertriangle   Mark Twain


0




Reply

nospam

11/12/2010 8:05:11 PM


On Nov 12, 8:59=A0pm, steve <kar...@comcast.net> wrote:
> On Nov 12, 11:47=A0am, Daniel Carrera <dcarr...@gmail.com> wrote:
>
>
>
>
>
> > On Nov 12, 4:02=A0pm, Jugoslav Dujic <jdu...@yahoo.com> wrote:
>
> > > Take a look at Michel Olagnon's (who used to frequent clf):
>
> > >http://www.fortran2000.com/rank/
>
> > > In particular, INDMED and VALMED are based on Knuth. I used some
> > > of other Michel's routines, but not these ones.
>
> > Thanks. I'll take a look at VALMED. In fact, I have a question
> > already. VALMED starts with:
>
> > Recursive Function D_valmed (XDONT) Result (res_med)
>
> > I thought that function definitions always needed a return type like
> > "real function foo()". When I compile my own code with gfortran it
> > always complains if I don't include a return type, but Michel's module
> > compiles just fine the way it is. What's Michel doing that allows him
> > to get away with a function without a return type?
>
> Removing the large comment from Michel's code, I finds
> the first 3 lines:
>
> Recursive Function D_valmed (XDONT) Result (res_med)
> =A0 =A0 =A0 Real (kind=3Dkdp), Dimension (:), Intent (In) :: XDONT
> =A0 =A0 =A0 Real (kind=3Dkdp) :: res_med
>
> The return type is declared in line 3.
Interesting. I didn't know you could do that. I was beginning to think
that he was relying on implicit types in some way. I notice that he
doesn't use "IMPLICIT NONE".
Thanks. I learned something new today.
Daniel.


0




Reply

Daniel

11/12/2010 8:16:08 PM


On Nov 12, 9:05=A0pm, nos...@see.signature (Richard Maine) wrote:
> He presumably declares the type later in the code in a separate
> statement. Not only is that allowed, it is what I prefer and recommend.
Yes, he does. I didn't realize that was possible until Steve pointed
it out.
> There can be problems with declaring the type directly on the function
> statement; it works in most cases, but not all of them. Declaring the
> type in a separate type declaration statement works in *ALL* cases. I
> prefer to do things that work all the time so that I don't need to worry
> about checking when I have crossed the boundary between the cases that
> work and the ones that don't.
Sounds reasonable. I'm surprised that declaring the type on the
function statement can fail. Can you give me an example? Anyway, I
certainly want to use methods that work all the time.
> If your compiler complains when you omit the type, then probably you are
> using implicit none (which I do recommend). Either that or you are doing
> something that is incompatible with the implicit type.
Yes, I always use implicit none. I only have a few weeks knowledge of
Fortran 95, but I learned to use implicit none from the very
beginning. Implicit typing scares me. I'm surprised that Michael
doesn't use implicit none.
> I wouldn't mention this except that
> I wonder why you would ever be trying to write code that doesn't declare
> the return type and also isn't deliberately using implicit typing; the
> only reason I can think of (other than just an accident) is that one
> might think that failing to explicitly declare the type somehow left it
> generic.
It was just an accident. I just forgot to enter the type once and saw
the compiler error. I just "understood" that every function needs a
return type, and I was surprised when I didn't see one in Michael's
code. I never thought of implicit typing (a concept that still seems
very odd to me).
Cheers,
Daniel.


0




Reply

Daniel

11/12/2010 8:30:35 PM


Daniel Carrera <dcarrera@gmail.com> wrote:
> On Nov 12, 9:05�pm, nos...@see.signature (Richard Maine) wrote:
>> He presumably declares the type later in the code in a separate
>> statement. Not only is that allowed, it is what I prefer and recommend.
> Yes, he does. I didn't realize that was possible until Steve pointed
> it out.
>> There can be problems with declaring the type directly on the function
>> statement; it works in most cases, but not all of them. Declaring the
>> type in a separate type declaration statement works in *ALL* cases. I
>> prefer to do things that work all the time so that I don't need to worry
>> about checking when I have crossed the boundary between the cases that
>> work and the ones that don't.
> Sounds reasonable. I'm surprised that declaring the type on the
> function statement can fail. Can you give me an example? Anyway, I
> certainly want to use methods that work all the time.
The separate return variable is required for direct recursion.
(When a function calls itself directly.) Inside a function without
a separate return variable, either declared on the FUNCTION statement
or in a separate statement, the function name is a variable, and
not a function reference.
 glen


0




Reply

glen

11/12/2010 8:43:13 PM


Daniel Carrera <dcarrera@gmail.com> wrote:
> On Nov 12, 9:05 pm, nos...@see.signature (Richard Maine) wrote:
> > There can be problems with declaring the type directly on the function
> > statement; it works in most cases, but not all of them. Declaring the
> > type in a separate type declaration statement works in *ALL* cases. I
> > prefer to do things that work all the time so that I don't need to worry
> > about checking when I have crossed the boundary between the cases that
> > work and the ones that don't.
>
> Sounds reasonable. I'm surprised that declaring the type on the
> function statement can fail. Can you give me an example? Anyway, I
> certainly want to use methods that work all the time.
Looking at the code in question, I see that it isn't actualy too far
from the main case that I think fails. Basically, the problems come from
ordering requirements. The function statement is always the first
statement in the function; there are no exceptions to that.
There are some things that you can't use until after the statement that
defines them. If you want to use any of those things on the function
statement, there can be a problem in that the function statement is not
likely to be after the statement where they are defined.
A derived type is one of those things. Normally you can't use a derived
type until after its definition. I think (not worth checking, but I
think it is right) that a specialcase exception was made for using a
derived type in a function statement just in order to allow it. I don't
tend to like such special cases and I'd have probably voted against
making one for this (don't recall whether I ever got a chance to make
such a vote  might have). Special cases tend to grow as one discovers
that you didn't catch quite everything. Indeed, this special case didn't
catch everything.
I think the missing case is type parameters. One often (and preferably)
specifies type parameter values using named constants. I do no think
there is a simillar exception to allow using a named constant in a
function statement before its definition. So if you try to do something
like
real(my_kind) function f(x)
use module_with_a_definition_of_my_kind
...
I think you are out of luck. It is possible that I've gotten confused on
the details and that this particular case works. But if it isn't exactly
this one, it is something simillar. One of the advatanges of doing the
type declararation in a separate statement is that I don't actually have
to keep track of the fine points about exactly which cases work.
Posted excerpts of the posted code do show that it uses a named constant
for a kind type parameter, so it is at least close to this case. I don't
see a USE in the posted extract though, so perhaps the kind type
parameter might be accessed via host association. That would be a case
that was ok.... well as long as it wasn't an interface body.

Richard Maine  Good judgment comes from experience;
email: last name at domain . net  experience comes from bad judgment.
domain: summertriangle   Mark Twain


0




Reply

nospam

11/12/2010 9:21:31 PM


On Nov 12, 10:21=A0pm, nos...@see.signature (Richard Maine) wrote:
> A derived type is one of those things. Normally you can't use a derived
> type until after its definition...
>
> I think the missing case is type parameters. One often (and preferably)
> specifies type parameter values using named constants. I do no think
> there is a simillar exception to allow using a named constant in a
> function statement before its definition. So if you try to do something
> like
>
> =A0 real(my_kind) function f(x)
> =A0 =A0 use module_with_a_definition_of_my_kind
> =A0 =A0 ...
>
> I think you are out of luck.
I just found another example. Suppose you want a function that returns
an array:
function myfunc() result(res)
real, dimension(3) :: res
res =3D (/ 2.3, 2.4, 2.5 /)
end function
This compiles an runs correctly. But when I tried move the type
declaration to the function definition, the compilation failed.
Cheers,
Daniel.


0




Reply

Daniel

11/12/2010 10:38:25 PM


Daniel Carrera <dcarrera@gmail.com> wrote:
> I just found another example. Suppose you want a function that returns
> an array:
>
> function myfunc() result(res)
> real, dimension(3) :: res
> res = (/ 2.3, 2.4, 2.5 /)
> end function
>
> This compiles an runs correctly. But when I tried move the type
> declaration to the function definition, the compilation failed.
Good point, thought I'll make a minor terminology correction. You
actually can still put the type into the function statement. You just
can't put the dimension attribute there. The dimension is not part of
the type even though you normally put it in the type declaration
statement; it is an attribute.
You can do (and I just checked to make sure that I hadn't missed
something  worked fine).
real function myfunc() result(res)
dimension :: res(3)
res = (/ 2.3, 2.4, 2.5 /)
end function
I do not recommend that at all. I personally find it abominable. But it
is standard conforming.
Now that you mention attributes, that reminds me that things like the
pointer attribute have simillar issues in not being specifiable on the
function statement. But I don't even want to mention that it is a
possibility for a function to return a pointer because that might prompt
someone to write such functions, which I advise against. Pretend I
didn't say anything. :)
Functions that return allocatables are a much better example in that I
think they can be good ideas.

Richard Maine  Good judgment comes from experience;
email: last name at domain . net  experience comes from bad judgment.
domain: summertriangle   Mark Twain


0




Reply

nospam

11/13/2010 12:54:35 AM


Hi, I didn't read everything above.
But a search did not find the term "partial".
IMHO, quantiles, median belongs to it, are fastest found with "partial
sorting".
"Partial sorting" means that the data are not fully sorted, it is only
sorted so far that all smaller values are before the median and all
larger
values are behind, but they are not exactly sorted.
There are good algorithms available if you just look for
"partial sorting".
Wolfgang
On Nov 12, 7:54=A0pm, nos...@see.signature (Richard Maine) wrote:
> Daniel Carrera <dcarr...@gmail.com> wrote:
> > I just found another example. Suppose you want a function that returns
> > an array:
>
> > =A0 =A0 function myfunc() result(res)
> > =A0 =A0 =A0 =A0 real, dimension(3) :: res
> > =A0 =A0 =A0 =A0 res =3D (/ 2.3, 2.4, 2.5 /)
> > =A0 =A0 end function
>
> > This compiles an runs correctly. But when I tried move the type
> > declaration to the function definition, the compilation failed.
>
> Good point, thought I'll make a minor terminology correction. You
> actually can still put the type into the function statement. You just
> can't put the dimension attribute there. The dimension is not part of
> the type even though you normally put it in the type declaration
> statement; it is an attribute.
>
> You can do (and I just checked to make sure that I hadn't missed
> something  worked fine).
>
> =A0 =A0 real function myfunc() result(res)
> =A0 =A0 =A0 =A0 dimension :: res(3)
> =A0 =A0 =A0 =A0 res =3D (/ 2.3, 2.4, 2.5 /)
> =A0 =A0 end function
>
> I do not recommend that at all. I personally find it abominable. But it
> is standard conforming.
>
> Now that you mention attributes, that reminds me that things like the
> pointer attribute have simillar issues in not being specifiable on the
> function statement. But I don't even want to mention that it is a
> possibility for a function to return a pointer because that might prompt
> someone to write such functions, which I advise against. Pretend I
> didn't say anything. :)
>
> Functions that return allocatables are a much better example in that I
> think they can be good ideas.
>
> 
> Richard Maine =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Good judgment come=
s from experience;
> email: last name at domain . net  experience comes from bad judgment.
> domain: summertriangle =A0 =A0 =A0 =A0 =A0  =A0 Mark Twain


0




Reply

wolfgang

11/13/2010 9:15:56 AM


On 11/12/2010 09:30 PM, Daniel Carrera wrote:
> On Nov 12, 9:05 pm, nos...@see.signature (Richard Maine) wrote:
>> He presumably declares the type later in the code in a separate
>> statement. Not only is that allowed, it is what I prefer and recommend.
>
> Yes, he does. I didn't realize that was possible until Steve pointed
> it out.
>
>
>> There can be problems with declaring the type directly on the function
>> statement; it works in most cases, but not all of them. Declaring the
>> type in a separate type declaration statement works in *ALL* cases. I
>> prefer to do things that work all the time so that I don't need to worry
>> about checking when I have crossed the boundary between the cases that
>> work and the ones that don't.
>
> Sounds reasonable. I'm surprised that declaring the type on the
> function statement can fail. Can you give me an example? Anyway, I
> certainly want to use methods that work all the time.
>
>
>> If your compiler complains when you omit the type, then probably you are
>> using implicit none (which I do recommend). Either that or you are doing
>> something that is incompatible with the implicit type.
>
> Yes, I always use implicit none. I only have a few weeks knowledge of
> Fortran 95, but I learned to use implicit none from the very
> beginning. Implicit typing scares me. I'm surprised that Michael
> doesn't use implicit none.
>
Well, I write a lot of quick and dirty programs, but when it is code
that I want to keep or publish, I do check for undeclared variables.
In the case of the sorting and ranking routines, I compiled them with an
F compiler, that enforces implicit none, so everything is declared.
Regarding the way declarations are arranged, some features come from the
fact that I used a preprocessor to build the various type versions of
the routines, by lack of a suitable mechanism in the language (at least
at that time, I don't know if something has appeared now).
The code looked at first like:
Module m_mrgrnk
Integer, Parameter :: kdp = selected_real_kind(15)
public :: mrgrnk
private :: kdp
private :: R_mrgrnk, I_mrgrnk, D_mrgrnk
interface mrgrnk
module procedure D_mrgrnk, R_mrgrnk, I_mrgrnk
end interface mrgrnk
contains
Subroutine D_mrgrnk (XDONT, IRNGT)
$define MYTYPE "Real (kind=kdp)"
$include "mrgrnk.inc"
End Subroutine D_mrgrnk
Subroutine R_mrgrnk (XDONT, IRNGT)
$define MYTYPE "Real"
$include "mrgrnk.inc"
End Subroutine R_mrgrnk
Subroutine I_mrgrnk (XDONT, IRNGT)
$define MYTYPE "Integer"
$include "mrgrnk.inc"
End Subroutine I_mrgrnk
End Module m_mrgrnk


0




Reply

Michel

11/15/2010 8:20:52 AM


On 11/12/2010 04:02 PM, Jugoslav Dujic wrote:
>
> On 12.11.2010. 10:52, Daniel Carrera wrote:
>> Thanks for the help.
>>
>> Now I know that I need to write my own function. Thanks for the
>> suggestions for algorithms. I'll try to pick something that I
>> understand, but is efficient. I have an idea for an algorithm based on
>> the quicksort / mergesort idea. Probably not as efficient as James'
>> code, but it should be good enough, and it'll be something I
>> understand.
>>
>> Thanks for the help.
>
> Take a look at Michel Olagnon's (who used to frequent clf):
>
> http://www.fortran2000.com/rank/
>
> In particular, INDMED and VALMED are based on Knuth. I used some
> of other Michel's routines, but not these ones.
>
I programmed Knuth's algorithm for the sake of sport, but in practice it
is so complex that you would have to go to huge values of N to see some
advantage over the simple pivoting strategy.


0




Reply

Michel

11/15/2010 8:27:08 AM


In article <8kc96tFfv4U1@mid.individual.net>,
Michel Olagnon <molagnon@ifremeraoter.fr> wrote:
>
>I programmed Knuth's algorithm for the sake of sport, but in practice it
>is so complex that you would have to go to huge values of N to see some
>advantage over the simple pivoting strategy.
That is true, but the statistical one is enough simpler and faster
that you start to see gains at quite low values  if I recall, it's
about 2025.
Regards,
Nick Maclaren.


0




Reply

nmm1

11/15/2010 8:49:44 AM



22 Replies
3345 Views
Similar Articles
[PageSpeed]
58

Similar Artilces:
Rank the arrary, then Find the duplicated values in the array and replace rank value with the mean of the ranks for the duplicated elements in Array.for example:
Array Rank replaced_rank
  
1 1 (1+2)/2
1 2 (1+2)/2
2 3 (3+4+5)/3
2 4 (3+4+5)/3
2 5 (3+4+5)/3
3 6 6
the 'replaced_rank' array is what I needed.
how to get it?
"yu " <mothee.yue@gmail.com> wrote in message <jluclt$r47$1@newscl01ah.mathworks.com>...
> for example:
>
> Array Rank replaced_rank
>   
> 1 1 (1+2)/2
> 1 ... finding values in one array and using those locations to change values in one channel of a separate 3 channel arraySo I have a labeled array (array1) with connected regions of interest (background is all zeros, connected regions are all 1's for the first region, all 2's for the second, 3's for the 3rd etc.) I also have a vector (vector1) of the region labels I find important (ex. 1,6,9). I want to find the locations of these values in the labeled array and then change values in one channel of a separate 3 channel array at the same locations (want to color certain parts of an image green based regions of interest found in another image).
I can use the below code to change all channels, but do... Find a closest value of array and test which of the values gives the highest function value as below...Hello all, i have one problem, if someone helps me, i will really appreciate. So please, HELP ME !
I have one array, let's say it is is like this:
A=[3, 5, 4, 6.2, 7.1, 7.8, 9.3, 10, 11, 13]
I have to find the closest value of 7 (<=7). And then, i have to search in this vector for all values that are smaller than the closest value that we found (from the value that we found in descending order one by one search till the first value of array), and to see that which of these values gives as maximum value of function f(i), the function is like below:
f(i) = 5/i+i/6+7/i
So, i have... Finding lowest numeric value in an array with undef valuesSuppose I have an array:
my @array = (1, 2, undef, 3);
I want to find the smallest numeric value (ignoring undef values). My
first inclination is to use the min() method of List::Util, but the
undef value causes problems (the method throws a warning and then
returns the undef).
I could do this:
use List::Util qw{ min };
print min ( grep /\d+/, @array );
which returns the expected value (1), but strikes me as kludgy (and
still throws a warning).
Any better suggestions?
Thanks!

The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com... Finding the two closest array values to some arbitrary valueI would like to identify the row indices of two minimum values in a cell array, containing data Zi. Searching for a single value is simple (see below) but how to I search for the minimum and the closest value to that minimum?
minimum value search:
c=find(min(Z)==Z); %find indices
"Daniel " <elefant2_2000@yahoo.de> wrote in message <hdud6u$dpn$1@fred.mathworks.com>...
> I would like to identify the row indices of two minimum values in a cell array, containing data Zi. Searching for a single value is simple (see below) but how to I search for the minim... finding a set of values in another set array of valueshi
i'm trying to find how to find a set of values in another array set of values...
lets say i have:
pf is an array of values which is structured 1x46
pf(1)=[61,62,63,64,65]
pf(2)=[62,63,64,65,61]
pf(3)=[63,64,65,61,62]
....
pf(46)=[...]
and so on...
what i need to find is: among these sets of pf from index 1 to 46, i just want to extract indexes of those with values [61,62,63,64,65]
thnx.
nora
"Nora Ramli" <noraznita86@hotmail.com> wrote in message <h6b1cp$774$1@fred.mathworks.com>...
> hi
>
> i'm trying to find how to find a set of values in ... Given an array a[N] with value 1...N, find a repeted valueAnyone has the solution to the question "Given an array a[N] with value
1...N, find a repeted value" in C code?
Thanks a lot
<cai_rongxi@hotmail.com> wrote in message
news:1103992195.221718.104740@c13g2000cwb.googlegroups.com...
> Anyone has the solution to the question "Given an array a[N] with value
> 1...N, find a repeted value" in C code?
>
> Thanks a lot
My solution is to ask your class teacher.
cai_rongxi@hotmail.com wrote:
> Anyone has the solution to the question "Given an array a[N] with value
> 1...N, find a repeted value" ... Finding values in an arrayI have an array/matrix which is 10x365, and I need to find a way to search for the values under a certain value, eg less than 0, then tell me how many values in the are less than zero.
Thanks for any help.
On 6/14/2013 12:46 PM, Brian wrote:
> I have an array/matrix which is 10x365, and I need to find a way
>to search for the values under a certain value, eg less than 0, then
>tell me how many values in the are less than zero.
>
> Thanks for any help.
>
may be
EDU>> A=rand(5);
EDU>> length(find(A<0.5==1)) %how many less than 0.5?
1... Finding values (text,numbers, regexps) in objects (arrays or key/value pairs)I have an array of objects and I want to retrieve specific elements
that match certain criteria, i.e. find value A in an object B
The problem is that I cannot know what is A (text, object, reg exp,
array) and what is B (text, object, array).
Also, I must assume that B may be an array or a key/value object and
that A may be anywhere (including 'children' objects).
This is usually the case when I have a JSON response from an external
source and want to find something in there but without hardcoding any
specific methods or search cases.
I think the best way to achieve this is to have... Find values in an arrayHi all,
Have been using this code for years to add unique values to an array. Is
there a better way  faster?
For z = 1 To cntTbl
If tmpTblSpc = TblSpc(z) Then
TblFnd = True
Exit For
End If
DoEvents
Next z
If TblFnd = False Then
cntTbl = cntTbl + 1
TblSpc(cntTbl) = tmpTblSpc
TblFnd = False
txtLog = txtLog & tmpTblSpc & vbCrLf
End If
Thanks
Dan...
On Tue, 6 Apr 2004 14:47:39 0400, "Dan E." <formulazss@twcny.rr.com> ... Replacing some # of values of an array with a different array of same # of valuesHi all,
I hope my title makes sense. I have a feeling that what I am trying to do is not necessarily difficult, but I'm just not sure how to approach this problem without doing something extremely inefficient.
I have an array of values, e.g. :
temp = [1 2 3 50 1 2 3 60 1 2 3 50 1 2 3];
I want to find all instances of '50' and '60' and replace them with alternative values.
altern = [80 70 80];
The number of values that I identify in temp will always match the number of alternative values that I want to insert.
I was thinking that I would use find() to lo... Find Values in an arrayHi there,
I want to find the indices of a specific value say 78.0577 in an array called ABC. I thought the code was:
>ind=find(ABC==78.0577)
however the result I get given back is:
ind =
Empty matrix: 0by1
What am I doing wrong?
Many thanks
On Oct 21, 10:00=A0am, "Pete Davis" <peted.da...@gmail.com> wrote:
> Hi there,
>
> I want to find the indices of a specific value say 78.0577 in an array ca=
lled ABC. I thought the code was:
>
> >ind=3Dfind(ABC=3D=3D78.0577)
>
> however the result I get given back is:
> ind =3D
>
> =A0 =A0Empty... Find an array in an arrayHi
In excel sheet, I have the column like this:
0
0
0.001(error data)
0
0
10
20
30
50
20
5
0
0
0.002(error data)
0
0
0
I would like to pick up only this array (set of numbers between zeros)
0
10
20
30
50
20
5
0
and would like to know where they are.
Thank you in advance!!
atk
On 6/25/2012 10:43 PM, ATSUKI OTO wrote:
> Hi
>
> In excel sheet, I have the column like this:
>
> 0
> 0
> 0.001(error data)
> 0
> 0
> 10
> 20
> 30
> 50
> 20
> 5
> 0
> 0
> 0.002(error data)
> 0
> 0
&... try to find median valuei have trouble about finding the median value for my vector.. i have 1x30000 vector and have been sorted.. how can i get the value of median? thanks for help :)
"Ermitha" wrote in message <jm0no8$2vr$1@newscl01ah.mathworks.com>...
> i have trouble about finding the median value for my vector.. i have 1x30000 vector and have been sorted.. how can i get the value of median? thanks for help :)
help median
Bruno
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jm0png$9jt$1@newscl01ah.mathworks.com>...
> "Ermitha" wrote in mes... Find value in an array/matrixHi all
I was wondering if there is a way to search within a matrix
of 2x10
This search would point out a value which has
repeated within the same row 3 times. This value
could have a variation of +/0.5. The output of the
system would be the average of these three values.
thought of using a coulple of if conditions, but I'm
not sure how to start.
Any suggestions are appreciated
This value
> could have a variation of +/0.5.
>
A little clearing up... Do you mean that the values should
be the same within .5 or that on different arrays, the
values would vary. Maybe provide an ... array of hashes (finding value)I'm just looking for other possible solutions. Sorry, this is kinda a
noob question.
Currently I loop through the array and check the value of each hash key
to find values. Is that the only and/or best way to find matches?
example:
@invoices = [{:id => 1, :first_name => 'nate'}, {:id => 2, :fist_name =>
'greg'}, {:id => 3, :first_name =>}]
@invoices.each do invoice
if invoice[:id] == params[:contact_id]
bla... bla...
end
end

Posted via http://www.rubyforum.com/.
[Note: parts of this message were removed to make it a legal post.]
On... finding dominant value in an array
I'm looking to implement a subroutine or function that accepts a
vector of integers and returns the dominant (most prominent) value.
For example, passed 7,3,9,7,7,2,4,1 it will return 7. Can't seem to
envision anything simple and elegant. Anyone have code that does this
or a pointer in the right direction? TIA.
On Nov 11, 10:47=A0am, Mike <rawlin...@yahoo.com> wrote:
> I'm looking to implement a subroutine or function that accepts a
> vector of integers and returns the dominant (most prominent) value.
> For example, passed 7,3,9,7,7,2,4,1 it will return ... how do i find the max value out of an array?Hi,
i have a text file which is comma seperated and i have extracted few
values from the text in an array for example,
30, 1, 4,5, 31, 4, 2, 3, 32, 2,3,0, 38
my goal is to find the max of 30, 31, 32, 38 (i, i+4, i + 8, i+
12....so on)
i have put a split statement and in a loop and captured these 4
elements in an array. how do i find the max of these values, which is
in an array using perl?
thanks
IJALAB <balaji.draj@gmail.com> wrote:
>i have a text file which is comma seperated and i have extracted few
>values from the text in an array for example,
>30, 1, 4,... Find minimum value in arrayI need to find the minimum value in an array and return an array [m n] where m is the smallest number and where n is the position in the array. I need to use recursion. This is what i have so far:
function [m n] = recursiveMin(A)
size = length(A);
if isempty(A)
m = [];
n = [];
elseif size == 1
m = A(1);
n = 1;
else
A(1) = max;
if
%this is where im stuck
end
end
Thanks for the help
And the reason you're not using the builtin min() function is.......
"John " <Nickg5859@gmail.com> wrote in message &... to find a certain value in an arrayI have an array named th
actually it is a structure, so the values of th is in th.signals.values but i guess that is the same thing.
the numbers in th are increasing and i want to know when the first certain value occurs.
to be more exact i want to know what position in the array the holds the value 8400, however there are many values of 8400, but i want to know the first (counting form the top of the array)
im looking for something similar to the command find which finds and returns the indices of all zeros of an array, but i want it to find and return the indices of all values of 8... Find display value in arrayHi,
I have a array that looks like this
Type = [["key","value"],["key..","value.."],["..",".."]]
Now i want to the the display (value) when i have the key, is this a OK
way to do it? Or is there a better way?
Type.select{key,disp key == "H" }.collect{key,valuevalue}.to_s

Posted via http://www.rubyforum.com/.
On Jul 10, 2007, at 7:10 AM, Jon Stenqvist wrote:
> Hi,
>
> I have a array that looks like this
> Type = [["key","value"],["key..","value.."],["..&... Finding a value within an arrayGiven the array A={1 2 3; 'aa' 'bb' 'cc'; 4 5 6}, I would like to be able to find the index of an input variable, where let's say input=1.
I tried to use [row,col]=find(A==input). However this gives the error message "Undefined function or method 'eq' for input arguments of type 'cell'."
I also tried to use find(strcmp(A, input)) but that also gives an error since the input 1 is not a string.
Do you have any suggestions?
On 6/24/2012 2:18 PM, conan.so@gmail.com wrote:
> Given the array A={1 2 3; 'aa' 'bb' '... Finding a value in a array efficientlyI was asked an apparently simple question  what is the most efficient
way in IDL to determine if a particular scalar value is in an array.
The loop method is straightforward:
found = 0
for i=0,n1 do begin
if array[i] EQ value then begin
found = 1
goto, DONE
endif
endfor
DONE:
This ugly code will end the loop as soon as a match is found.
A first attempt at vectorized code might be
index = where(array EQ value, ng)
found = ng gt 0
But this code both creates an unnecessary index array, and requires
searching the entire array even if the match is found in ... output array index from comparing input value to array valueHi, i am trying to get an array index value by comparing an input value to the values in the array. the closest value in the array to the input would result in the output being the index of the array. for example array value index0 07 &...



