[Java] Matrice differenza elementi vicini

Messaggioda Bazzaz » 10/06/2020, 13:42

Salve a tutti ho un problema con questo esercizio in java (o meglio con un punto di un esercizio)

intanto ecco il codice del programma
Testo nascosto, fai click qui per vederlo
Codice:
import java.math.*;
import java.util.Random;
public class e1 {
public static void main(String[] args) {

    double [] [] matrice;
    double [] arr;
    double somma = 0;

   

    matrice = generaMatrice(5, 5);
    System.out.println("stampo matrice");
    stampaMatrice(matrice);

    arr =  diagonale(matrice);
    System.out.println("Stampo array");
    stampaArr(arr);
   
    somma = sommaR(arr);
    System.out.println("Stampo somma: "+somma);

    double [][] matrice2 = diffVicini(matrice);
    System.out.println("stampo matrice");
   
    stampaMatrice(matrice2);
}

public static double[][] generaMatrice(int righe,int colonne){

    double [][] mat = new double [righe][colonne];
   

    for(int i = 0; i<righe;i++){
        for(int j = 0;j<colonne;j++){
            mat[i][j] = (double)(Math.random()*100);
        }
    }
    return mat;
 }

 public static void stampaMatrice(double[][]matrice){
     for(int i = 0; i<matrice.length;i++){
         for(int j=0;j<matrice[0].length;j++){
             System.out.print(matrice[i][j]+ " ");
         }
         System.out.println();
     }
 }
 public static double [] diagonale(double[][]matrice){
     double [] array = new double [matrice.length];

     for(int i = 0;i<matrice.length;i++){
         for(int j = 0; j<matrice.length;j++){
             if(j == i){
                 array[j] = matrice[i][j];
             }
         }
     }
     return array;
 }
 public static void stampaArr(double []arr){
     for(int i = 0;i<arr.length;i++){
         System.out.print(arr[i]+" ");
     }
 }

public static double sommaR(double[]arr){
    double s = 0;
    for(int i = 0; i<arr.length;i++){
        s+=arr[i];
    }
    return s;
}
public static double[][] diffVicini(double[][]matrice){
    double [][] matriceDiff = new double[matrice.length][matrice[0].length];
    double sup,inf,sx,dx,supSx,supDx,infSx,infDx;
    for(int i = 0; i<matrice.length;i++){
        for(int j = 0; j<matrice[0].length;j++){

            if(j == 0&& i == 0){
                dx = matrice[i][j+1];
                inf = matrice[i+1][j];
                infDx = matrice[i+1][j+1];
                matriceDiff[i][j] = matrice[i][j]-dx-inf-infDx;
            }else if(i == matrice.length-1 && j == 0){
                sup = matrice[i-1][j];
                supDx = matrice[i-1][j+1];
                dx = matrice[i][j+1];
                matriceDiff[i][j] = matrice[i][j]-sup-dx-supDx;

            }else if(i == 0 && j == matrice[0].length-1){
                sx = matrice[i][j-1];
                inf = matrice[i+1][j];
                infSx = matrice[i+1][j-1];
                matriceDiff[i][j] = matrice[i][j]-sx-inf-infSx;

            }else if(i == matrice.length-1 && j == matrice[0].length-1){
                sup = matrice[i-1][j];
                supSx = matrice[i-1][j-1];
                sx = matrice[i][j-1];
                matriceDiff[i][j] = matrice[i][j]-sx-supSx-sup;
            }else if((j>0)&&(j<matrice[0].length)){
                if(i==0){
                     inf = matrice[i+1][j];
                     dx = matrice[i][j+1];
                     sx = matrice[i][j-1];
                     infDx = matrice[i+1][j+1];
                     infSx = matrice[i+1][j-1];
                     matriceDiff[i][j] = matrice[i][j]-sx-inf-infSx-dx-infDx;
                }else if(i == matrice.length-1){
                    sup = matrice[i-1][j];
                    dx = matrice[i][j+1];
                    sx = matrice[i][j-1];
                    supDx = matrice[i-1][j+1];
                    supSx = matrice[i-1][j-1];
                    matriceDiff[i][j] = matrice[i][j]-sx-sup-supDx-dx-supSx;
                }


            }else if(j==0){
                if(i> 0 && i <matrice.length-1){
                    sup = matrice [i-1][j];
                    inf = matrice [i+1][j];
                    supDx = matrice [i-1][j+1];
                    infDx = matrice [i+1][j+1];
                    dx = matrice [i][j+1];
                    matriceDiff[i][j] = matrice[i][j]-sup-inf-supDx-infDx-dx;
                   
                }
            }else if(j == matrice[0].length-1){
                if(i> 0 && i <matrice.length-1){
                    sup = matrice [i-1][j];
                    inf = matrice [i+1][j];
                    supSx = matrice [i-1][j-1];
                    infSx = matrice [i+1][j-1];
                    sx = matrice [i][j-1];
                    matriceDiff[i][j] = matrice[i][j]-sup-inf-supSx-infSx-sx;
                   
                }
            }
            else{
               
                matriceDiff[i][j] = 1;

            }

        }
    }


    return matriceDiff;
}
   
}


Il problema ce l'ho con questo metodo
praticamente data una matrice in ingresso devo restituire una matrice il cui elemento i,j è dato dalla differenza degli elementi vicini quindi se ho una matrice

123
456
789

la nuova matrice per esempio in 0.0 sarà data da : 1 -2 -5 -4 = -10
(ovviamente se sono sui bordi non devo considerare gli indici che vanno fuori dalla matrice)

Codice:
public static double[][] diffVicini(double[][]matrice){
    double [][] matriceDiff = new double[matrice.length][matrice[0].length];
    double sup,inf,sx,dx,supSx,supDx,infSx,infDx;
    for(int i = 0; i<matrice.length;i++){
        for(int j = 0; j<matrice[0].length;j++){

            if(j == 0&& i == 0){
                dx = matrice[i][j+1];
                inf = matrice[i+1][j];
                infDx = matrice[i+1][j+1];
                matriceDiff[i][j] = matrice[i][j]-dx-inf-infDx;
            }else if(i == matrice.length-1 && j == 0){
                sup = matrice[i-1][j];
                supDx = matrice[i-1][j+1];
                dx = matrice[i][j+1];
                matriceDiff[i][j] = matrice[i][j]-sup-dx-supDx;

            }else if(i == 0 && j == matrice[0].length-1){
                sx = matrice[i][j-1];
                inf = matrice[i+1][j];
                infSx = matrice[i+1][j-1];
                matriceDiff[i][j] = matrice[i][j]-sx-inf-infSx;

            }else if(i == matrice.length-1 && j == matrice[0].length-1){
                sup = matrice[i-1][j];
                supSx = matrice[i-1][j-1];
                sx = matrice[i][j-1];
                matriceDiff[i][j] = matrice[i][j]-sx-supSx-sup;
            }else if((j>0)&&(j<matrice[0].length)){
                if(i==0){
                     inf = matrice[i+1][j];
                     dx = matrice[i][j+1];
                     sx = matrice[i][j-1];
                     infDx = matrice[i+1][j+1];
                     infSx = matrice[i+1][j-1];
                     matriceDiff[i][j] = matrice[i][j]-sx-inf-infSx-dx-infDx;
                }else if(i == matrice.length-1){
                    sup = matrice[i-1][j];
                    dx = matrice[i][j+1];
                    sx = matrice[i][j-1];
                    supDx = matrice[i-1][j+1];
                    supSx = matrice[i-1][j-1];
                    matriceDiff[i][j] = matrice[i][j]-sx-sup-supDx-dx-supSx;
                }


            }else if(j==0){
                if(i> 0 && i <matrice.length-1){
                    sup = matrice [i-1][j];
                    inf = matrice [i+1][j];
                    supDx = matrice [i-1][j+1];
                    infDx = matrice [i+1][j+1];
                    dx = matrice [i][j+1];
                    matriceDiff[i][j] = matrice[i][j]-sup-inf-supDx-infDx-dx;
                   
                }
            }else if(j == matrice[0].length-1){
                if(i> 0 && i <matrice.length-1){
                    sup = matrice [i-1][j];
                    inf = matrice [i+1][j];
                    supSx = matrice [i-1][j-1];
                    infSx = matrice [i+1][j-1];
                    sx = matrice [i][j-1];
                    matriceDiff[i][j] = matrice[i][j]-sup-inf-supSx-infSx-sx;
                   
                }
            }
            else{
               
                matriceDiff[i][j] = 1;

            }

        }
    }


    return matriceDiff;
}
   

forse non è il massimo dell'efficienza ma funziona tutto almeno fino a questo punto

Codice:
}else if(j == matrice[0].length-1){
                if(i> 0 && i <matrice.length-1){
                    sup = matrice [i-1][j];
                    inf = matrice [i+1][j];
                    supSx = matrice [i-1][j-1];
                    infSx = matrice [i+1][j-1];
                    sx = matrice [i][j-1];
                    matriceDiff[i][j] = matrice[i][j]-sup-inf-supSx-infSx-sx;
                   
                }
            }
            else{
               
                matriceDiff[i][j] = 1;

            }


quando ho aggiunto questo pezzo non mi fa i calcoli ecco l'output che mi ritrovo

Immagine

mi ritrovo tutti quegli 0 quando invece dovrei ritrovarmi 1
e inoltre mi appare questo errore
Immagine

Sapete aiutarmi?
Bazzaz
New Member
New Member
 
Messaggio: 38 di 78
Iscritto il: 12/09/2019, 22:01

Re: [Java] Matrice differenza elementi vicini

Messaggioda Super Squirrel » 11/06/2020, 12:32

Premesso che non conosco Java e che non ho letto in modo approfondito il codice, penso che il tutto possa essere notevolmente semplificato, senza stare lì a considerare caso per caso.
Consideriamo il seguente schema

x x x
x o x
x x x

dove o rappresenta un generico elemento della matrice di indici $i$ e $j$, mentre le x rappresentano gli ipotetici elementi adiacenti.
Si può quindi ricorrere a qualcosa del genere:
Codice:
for(int i_2 = i - (i != 0); i_2 <= i + (i != RIG - 1); ++i_2)
{
    for(int j_2 = j - (j != 0); j_2 <= j + (j != COL - 1); ++j_2)
    {
        ...

dove $RIG$ e $COL$ rappresentano rispettivamente il numero di righe e colonne della matrice.
Chi dorme in democrazia, si sveglia in dittatura.
Super Squirrel
Senior Member
Senior Member
 
Messaggio: 409 di 1486
Iscritto il: 16/05/2013, 22:05


Torna a Informatica

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite