Вопрос

Hi i use a hashMap containing integers as keys and list of matrix as values, here is the code

    import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import Jama.Matrix;


public class KFold {
public static HashMap<Integer,List<Matrix>> splitterDonnees(Matrix matReduite){
    int K =10;
    int unite = matReduite.getRowDimension()/K;
    int indexDebtest = 0;
    int indexFinTest = unite;
    Matrix matDonEntr = new Matrix(matReduite.getRowDimension()-unite,matReduite.getColumnDimension());
    Matrix matDonTest = new Matrix(unite,matReduite.getColumnDimension());
    HashMap<Integer,List<Matrix>> mapDonn = new HashMap<Integer,List<Matrix>>();

    for (int i =0;i<K;i++){
        int ireloaded = 0;
        int ireloadedEntr =0;
        for(int ii =indexDebtest; ii<indexFinTest;ii++){
            int jreloaded=0;
            for(int j =0;j<matReduite.getColumnDimension();j++){

                matDonTest.set(ireloaded, jreloaded, matReduite.get(ii, j));
                jreloaded++;
            }
            ireloaded++;

        }
        //matDonTest.print(10, 5);
        if(indexDebtest == 0){
            for(int ii =unite;ii<matReduite.getRowDimension();ii++){
                int jreloadedEntr=0;
                for(int j =0; j<matReduite.getColumnDimension();j++){
                    matDonEntr.set(ireloadedEntr, jreloadedEntr, matReduite.get(ii, j));
                    jreloadedEntr++;
                }
                ireloadedEntr++;
            }
        }

        else if (indexFinTest==matReduite.getRowDimension()){
            for(int ii =0;ii<matReduite.getRowDimension()-unite;ii++){
                int jreloadedEntr=0;
                for(int j =0; j<matReduite.getColumnDimension();j++){
                    matDonEntr.set(ireloadedEntr, jreloadedEntr, matReduite.get(ii, j));
                    jreloadedEntr++;
                }
                ireloadedEntr++;

            }
        }
        else{
            for(int ii=0;ii<indexDebtest;ii++){
                int jreloadedEntr=0;
                for(int j =0; j<matReduite.getColumnDimension();j++){
                    matDonEntr.set(ireloadedEntr, jreloadedEntr, matReduite.get(ii, j));
                    jreloadedEntr++;    
                }
                ireloadedEntr++;
            }
            for(int ii =indexFinTest;ii<matReduite.getRowDimension();ii++){
                int jreloadedEntr=0;
                for(int j =0; j<matReduite.getColumnDimension();j++){
                    matDonEntr.set(ireloadedEntr, jreloadedEntr, matReduite.get(ii, j));
                    jreloadedEntr++;

                }
                ireloadedEntr++;
            }
        }

        indexDebtest = indexDebtest+unite;
        indexFinTest = indexFinTest+unite;

        List<Matrix> coupleMatTest_MatEntr = new ArrayList<Matrix>();
        coupleMatTest_MatEntr.add(matDonTest);
        //matDonTest.print(10, 5);
        coupleMatTest_MatEntr.add(matDonEntr);
        mapDonn.put(i, coupleMatTest_MatEntr);
        //System.out.println("Les lements tests de mapDonnees sont:"+mapDonn.get(i).size());
        mapDonn.get(i).get(0).print(10, 5);
    }
    //mapDonn.get(0).get(0).print(10, 5);
    //mapDonn.get(1).get(0).print(10, 5);
    return mapDonn;
}
}

The problem is that the final version of the hashMaps that is returned contains 10 times the same last matrix, thing is it works well in the loop as i see the different values each times, but once i print the values just before the return, all the values are now of the same last matrix.

EDIT: The Matrix class is from the Jama Package.

Это было полезно?

Решение

Move the following two lines inside the loop:

Matrix matDonEntr = new Matrix(matReduite.getRowDimension()-unite,matReduite.getColumnDimension());
Matrix matDonTest = new Matrix(unite,matReduite.getColumnDimension());

This way you'll create new matrices on every iteration of the loop. Otherwise there are only these two instances in the method, and you're constantly changing them, ending in many references to the last state of them.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top