Re: Average of 2-D array

new topic     » goto parent     » topic index » view thread      » older message » newer message
irv said...

I have a two-dimensional sequence of integers (100 in all, arranged 10x10).

Let's call it x.

I can get math:sum(x) easily, it adds them up regardless of nesting. But stats:average(x) doesn't work that way.

I could get the average by dividing the sum by 100,but who knows if the array will always be that size? length(x) does not return the actual number of elements, it returns 10.

/usr/local/share/euphoria/include/std/stats.e:490 in function sum() type_check failure, result_ is {1,2,3,4,5,6,7,8,9,10}

Am I overlooking a function in either math or stats? Or a function to get the actual number of elements in the array?

No, you are not overlooking anything. The function you'd like to use is not in the standard library. If it did, it would look a lot like ...

 
public function count(object a) 
	atom b 
	if atom(a) then 
		return 1 
	end if 
	b = 0 
	for i = 1 to length(a) do 
		if atom(a[i]) then 
			b += 1 
		else 
			b += count(a[i]) 
		end if 
	end for 
	return b 
end function 

The average() function fails because it calls the stats:sum() function and not the maths:sum() function. Even though they have the same name, they have different purposes. The one in the stats library is designed for lists that may contain strings, which are incidental to the statistical requirements, such as a cell in a spreadsheet might have non-numeric data.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu