mardi 5 mai 2015

Comparing 2-dimension arrays in VB Excel

I'm trying to compare two 2d arrays in VBA Excel.

Source:

1 2 3 4

4 5 6 2

3 3 4 4

Target:

4 5 3 2

1 2 3 4

3 7 7 5

Given the above two 2-d arrays which I will call source and target I want to compare each row from source with entire target and check if it exists in target. For Example row 1 from source (1 2 3 4) would be considered a match as it would found in target (at row 2). So I need to compare each row in target for a given row from source. If row in source does not exist in target then I will need to make note of this some how in order to mark as not existing in target.

Something on the lines of (not actual code just idea):

For i to ubound(srcArray)
    isFound = False
    For j To ubound(trgArray)
        If srcArray(i) = trgArray(j) Then
            isFound = True

    If Not isFound Then
        //make note of some sort

I know approach worked ok for single dim. array. But trying to do this for 2d arrays in some sort of loop in VB or other method. Not too familiar with VB in Excel. I would also like to look at each row as entire array if possible rather than comparing each element for each array individually.

How can I give a certain row of elements all the same value? Using a 2d array?

If I have

String[][] trenches = new String[10][10];

How can i make all elements in row 0 have the value of "X" ? or all elements in row 1 have the value of "0" ?

I have 2 errors compiling my java Lottery program using arrays

import java.util.Random;
import java.util.Scanner;

public class LotteryGame{

public static void main(String[] args) {

    int NUM_DIGITS = 6;

    int[] userDigits = new int[NUM_DIGITS];
    int[] lotteryNumbers = new int[NUM_DIGITS];
    int sameNum;

    generateNumbers(lotteryNumbers);
    getUserData(userDigits);
    sameNum = compareArrays(lotteryNumbers, userDigits);

    System.out.println("Winning numbers: " + lotteryNumbers[0] + " "
            + lotteryNumbers[1] + " " + lotteryNumbers[2] + " "
            + lotteryNumbers[3] + " " + lotteryNumbers[4] + " " + lotteryNumbers[5] + " ");

    System.out.println("Your numbers:  " + userDigits[0] + " "
            + userDigits[1] + " " + userDigits[2] + " " + userDigits[3]
            + " " + userDigits[4] + " " + userDigits[5] +" ");
    System.out.println("Number of matching digits: " + sameNum);

    if (sameNum == 6) {
        System.out.println("First prize!!!");
    }

    if (sameNum == 5) {
        System.out.println("Second prize!!!");
    }
    if (sameNum == 0) {
        System.out.println("No matching numbers, you lost.");
    }

}

public static void generateNumbers(int[] lotteryNumbers) {

    Random randNum = new Random();

    lotteryNumbers[0] = randNum.nextInt(59);
    lotteryNumbers[1] = randNum.nextInt(59);
    lotteryNumbers[2] = randNum.nextInt(59);
    lotteryNumbers[3] = randNum.nextInt(59);
    lotteryNumbers[4] = randNum.nextInt(59);
    lotteryNumbers[5] = randNum.nextInt(59);

    return lotteryNumbers[5];
}

public static void getUserData(int[] userDigits) {
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter first digit: ");
    userDigits[0] = keyboard.nextInt();
    System.out.print("Enter second digit: ");
    userDigits[1] = keyboard.nextInt();
    System.out.print("Enter third digit: ");
    userDigits[2] = keyboard.nextInt();
    System.out.print("Enter fourth digit: ");
    userDigits[3] = keyboard.nextInt();
    System.out.print("Enter fifth digit: ");
    userDigits[4] = keyboard.nextInt();
    System.out.print("Enter sixth digit: ");
    userDigits[5] = keyboard.nextInt();



    return userDigits[5];
}

public static int compareArrays(int[] userDigits, int[] lotteryNumbers) {
    int sameNum = 0;

    for (int i = 0; i < 6; i++) {
        for (int x = 0; x < 5; x++) {
            if (lotteryNumbers[i] == userDigits[x]) {
                sameNum++;
            }
        }
    }
    return sameNum;
}

}

When I compile I get the following errors-

   LotteryGame.java:51: error: incompatible types: unexpected return value
       return lotteryNumbers[5];
                         ^
   LotteryGame.java:72: error: incompatible types: unexpected return value
    return userDigits[5];
                     ^
      2 errors

Can any of you help me with these compilation errors? I'm trying to get this to work. The user is supposed to input 6 numbers, and the program is supposed to pick randomly 6 numbers. Using these numbers, the program will compare the numbers with echoed input.

Sorting Array by biggest number

This is my unsorted array:

P  B    A 
5  125  400
2  102  145
3  56   200
6  65   200
7  30   200
4  148  300
1  135  0

This is what my current array has after I sorted it this is the output I get

P  B    A 
1  135  0  
4  148  0  
3  56  100  
2  102  100  
6  65  200  
5  125  200 

I want my array to sort B by the biggest number depending A like this example.

P  B    A 
4  148  0    
1  135  0  
2  102  100 
3  56  100  
5  125  200    
6  65  200

This is currently my code

 Arrays.sort(x, new Comparator<int[]>() {
                public int compare(int[] o1, int[] o2) {
                    int ret = Integer.compare(o1[2], o2[2]);
                    // if the entries are equal at index 2, compare index 1
                    if (0 == ret) {
                        ret = Integer.compare(o1[1], o2[1]);
                    }
                    return (ret);
                }
            });

How would i preform the tasks below in android studio?

1) Create a one dimensional array with 12 elements 2) set the first array values to be one 3) set every following array value to be the sum of the two before it 4) walk through the array and add up every element >= 10. 5) display the result as a toast message

I am having trouble doing this. I am very new to the android software and it would be great for some help.

Compute average of numpy array stack in Apache Spark

I'm working with large GeoTiff files and have loaded each GeoTiff as a numpy array, and then created an array-stack of the GeoTiffs:

GeoTiff_list = []
for i in range(1,n+1):
    GeoTiff_list.append(GeoTiff_i)
GeoTiff_array_stack = np.array(GeoTiff_list)  

Then I just want to compute the average by pixel across the stack of images. In numpy this can be done as follows:

average_GeoTiff = GeoTiff_array_stack.mean(axis=0)

I'd like to repeat this computation in spark. I know there are some projects in the pipeline, such as GeoTrellis, that will make this trivial...but I'm not sure how I can do it using whatever is currently available.

I presume I can parallelize the array as follow:

sc.parallelize(GeoTiff_array_stack)

But then I'm not sure how to compute the mean of this array. This is probably trivial in MLlib, but it seems like pyspark isn't up to speed with the the other spark APIs.

PHP: how to remove the last line break from the text file?

In a school work, I built a site for a fictional space museum in my city using PHP. It has a Data Inclusion and Data Consultation systems, but I have a problem with the consultation that I want to know how to solve: how to delete the last line break from the file?

Data Inclusion

In the restricted area of the site, I have a HTML5 form with 5 fields (name, addres, telephone number, sex and visited exhibitions) that sends the data by the method POST to a function in PHP that writes it on a given txt file by using the fwrite command:

fwrite ($pointer, "$name | $addres | $telephone | $sex | $exhibitions " .PHP_EOL);

As you can see, it writes in a txt file the data entered on the form, plus a line break. The pointer is the variable used by fopen to open the file that I need to work. Example of output:

Márcio Aguiar | Belmiro Braga Street | 1234-5678 | M | Planets of Solar System
Joana Tobias | Santos Dummont Avenue | 8765-4321 | F | Black Holes, Satellites

Data Consultation

Then there is a consultation system. It has a loop that runs until the file ends. Inside this loop there is a variable named $buffer that gets one line of the txt file each time. It is then exploded to create a array named $lines[$counter]. To print it nicely, I use a array_combine where I join the names of the fields on another array ($keys) to the values written in $lines[$counter], and attibutes that to $combined[$counter]. Then the loop ends and I use a print_r inside <pre></pre> to see the data written in $combined, while mantaining the spaces and breaks that HTML would otherwise ignore. Here is the code:

$keys = array ("Name", "Address", "Telephone", "Sex", "Visited exhibition");
for ($counter=0;!feof($reader);$counter++){
    $buffer = fgets($reader);
    $lines[$counter] = explode(" | ", $buffer);
    $combined[$counter] = array_combine($keys, $lines[counter]
}
echo "<pre>";
print_r($combined);
echo "</pre>";

Example of output:

    Array
    (
    [0] => Array
        (
            [Name] => Márcio Aguiar
            [Address] => Belmiro Braga Street
            [Telephone] => 1234-5678
            [Sex] => M
            [Visited exhibitions] => Planets of Solar System 

        )

    [1] => Array
        (
            [Name] => Joana Tobias
            [Address] => Santos Dummont Avenue
            [Telephone] => 8765-4321
            [Sex] => F
            [Visited exhibitions] => Black Holes, Satellites 

        )

    [2] => 
    )

Here you can see that a 2 Array was created blank. It's caused by the last line, that contains only a line break inserted by the form above. I need to remove this last line break, and only that one, but don't know how. I want to know! Not knowing causes the exhibition of an error when the execution arrive at the array_combine, because it's needed that the two arrays have the same number of elements, and 2 is blank. Here the error:

Warning: array_combine(): Both parameters should have an equal number of elements in E:\Aluno\Documents\Wamp\www\trab_1\area_restrita\consulta.php on line 60