I have the sums of x and y coordinates and I need to calculate the mean
float [][] sums = new float[n][2];
e.g. my array consists of:
{ [ 8,6 ],
[ 4,2 ] }
After the dividing to 2:
{ [ 4,3 ],
[ 2,1 ] }
I can do this with loops but I wonder if It is possible to do it with 1 function.
You can simply encapsulate your for loops into a function and call it:
import java.util.Arrays;
void setup(){
float [][] sums = new float[][]{ { 8,6 },
{ 4,2 } };
print("pre: ");
System.out.println(Arrays.deepToString(sums));
matMul(sums,0.5);
print("post:");
System.out.println(Arrays.deepToString(sums));
}
void matMul(float[][] data,float multiplier){
for(int y = 0; y < data.length; y++){
for(int x = 0; x < data[y].length; x++){
data[x][y] *= multiplier;
}
}
}
Here are a few pointers if you're just getting started with functions.
The simplest thing a function can do is encapsulate a bunch of instructions, but no allow any data from outside the function to be read and not return any data out.
Here is a very simple/boring example:
void sayHello(){
println("hello");
}
The point of is it to get familiar with the syntax. A function has:
- a (return) type: if the function returns any result, what kind of result is it ? (a number, a string of text, an Object, etc.), if not it's simply
void
- a name (so we can call it)
- arguments (0 or more) within parenthesis, each having a type and name (similar to a local variable)
- a body delinieated by the
{
and}
symbols: this everything defined within this block is only visible to this block (except what's returned, if anything). This is known as the scope of the function
You've probably already defined functions that don't return results: void setup(){}
, void draw(){}
You've probavly already called functions before: used the name and arguments (if any) in paranthesis (e.g. println("hi");
, point(50,50);
, noCursor();
, etc.)
Here's a simple example of a function that takes two inputs and returns one output: the sum of the inputs:
int sum(int a, int b){
return a+b;
}
Instead of void
we return a result of int
type and the arguments are a couple of integers named a and b. The only other extra thing is the return
keyword used to (you guessed it) return the result (and exit the function block).
Here a basic example calling the function:
void setup(){
println(add(2,2));
println(add(4,4));
}
int add(int a, int b){
return a + b;
}
If you want to read more on it check out Kevin Workman's Creating Functions Processing Tutorial
Bare in mind the the void method transforms the array reference to pass as an argument to the function. There may be situations where you don't want to change the input data, yet return the result. In such as case you can either copy the array beforehand and pass a copy, or have a version of the function that makes a copy for you:
import java.util.Arrays;
void setup(){
float [][] sums = new float[][]{ { 8,6 },
{ 4,2 } };
print("sums: ");
System.out.println(Arrays.deepToString(sums));
float[][] result = matMul(sums,0.5);
print("result: ");
System.out.println(Arrays.deepToString(result));
print("original:");
System.out.println(Arrays.deepToString(sums));
}
float[][] matMul(float[][] data,float multiplier){
// check input data
if(data == null || data.length == 0){
println("no data to multiply, returning null");
return null;
}
// make another array of the same shape
float[][] result = new float[data.length][data[0].length];
// do the multiplcation, storing the result into the duplicate array
for(int y = 0; y < data.length; y++){
for(int x = 0; x < data[y].length; x++){
result[x][y] = data[x][y] * multiplier;
}
}
// return the result
return result;
}
Notice a few checks at the start of the function: in general it's a good idea to validata data coming in, just in case, and display a message that will make it easy to fix the issue (withouth spending to much figuring out what the error means).