[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Sc-devel] maxIndex and minIndex?



would something like this work?

a  = [0, 3, 2, 5, 8, 3, 9, 6, 5]

a.indexOf(a.maxItem)
a.indexOf(a.minItem)

Josh

On Dec 27, 2007, at 2:54 AM, Click Nilson wrote:

Just wondering whether there is a method already for returning the index of the maximum or minimum element (rather than the value)? This is something I've often been recoding in algorithms and added my own extentions to SequenceableCollection as below; but perhaps there is some existing method I don't know the name of? Else can I add these? I have an interpolation constructor method as well that could go in as a convenience method rather than .series...

best,
Nick


+ SequenceableCollection {
*interp { arg size, start=0.0, end=1.0;
		var obj = this.new(size);
		
		if(size!=1,{
		size.do {|i|
		var t;
		
			t= i/(size-1);
			
			obj.add(start + (t*(end-start)));
		};
		},{obj.add(start);});
		
		^obj
}
maxIndex {

	var maxValue, maxElement, maxIndex;
			this.do { | elem,index|
				if (maxElement.isNil) {
					maxElement = elem;
					maxIndex = index;
				}{
					if (elem > maxElement) {
						maxElement = elem;
						maxIndex = index;
					}
				}		
			}
			^maxIndex;
}



minIndex {

	var minValue, minElement, minIndex;
			this.do { | elem,index|
				if (minElement.isNil) {
					minElement = elem;
					minIndex = index;
				}{
					if (elem < minElement) {
						minElement = elem;
						minIndex = index;
					}
				}		
			}
			^minIndex;
}


}
_______________________________________________
Sc-devel mailing list
Sc-devel@xxxxxxxxxxxxxxx
http://www.create.ucsb.edu/mailman/listinfo/sc-devel

******************************************
/* Joshua D. Parmenter
http://www.realizedsound.net/josh/

“Every composer – at all times and in all cases – gives his own interpretation of how modern society is structured: whether actively or passively, consciously or unconsciously, he makes choices in this regard. He may be conservative or he may subject himself to continual renewal; or he may strive for a revolutionary, historical or social palingenesis." - Luigi Nono
*/