Home > Java > javaTutorial > body text

Replace negative and positive numbers in matrix with 0 and 1 in Java

王林
Release: 2023-08-29 08:29:08
forward
1107 people have browsed it

Replace negative and positive numbers in matrix with 0 and 1 in Java

In Java, an array is an object. It is a non-primitive data type that stores data-like values. A matrix in java is nothing but a multidimensional array representing multiple rows and columns.

Here we are given a matrix which contains a set of elements including positive and negative numbers and as per the problem statement we have to replace negative numbers with 0 and positive numbers with 1.

Let’s delve into this article and learn how to do it using the Java programming language.

Show you some examples

Example 1

Given matrix=

-21 	22	-23
24	-25	26
-27	-28	29
Copy after login

After replacing negative numbers with 0 and positive numbers with 1,

The resulting matrix is ​​-

0 	1	0
1	0	1
0	0	1
Copy after login

Example 2

Given matrix=

-9 	2	-2	4
-1	-7	-2	6
2	-2	-4	3
-1	4	7	-8
Copy after login

After replacing negative numbers with 0 and positive numbers with 1,

The resulting matrix is ​​-

0 	1	0	1
0	0	0	1
1	0	0	1
0	1	1	0
Copy after login

Example 3

Given matrix =

-1 	-2	-3
4	5	6
-7	8	-9
Copy after login

After replacing negative numbers with 0 and positive numbers with 1,

The resulting matrix is: -

0 	0	0
1	1	1
0	1	0
Copy after login

algorithm

Algorithm 1

  • Step 1 - Create a 2D array matrix to store the numbers.

  • Step-2 - Call the replaceNum method to replace negative numbers in the matrix with 0 and positive numbers with 1.

  • Step-3 - Print the result matrix.

  • Step-4 - In the replaceNum method, use a for loop to iterate over the rows and columns of the matrix.

  • Step-5 - For each element in the matrix, use the ternary operator to replace the number with 0 or 1, depending on whether it is a negative or positive number.

    < /里>

Algorithm 2

  • Step 1 - Create a 2D array matrix to store the numbers.

  • Step-2 - Call the replaceNum method to replace negative numbers in the matrix with 0 and positive numbers with 1.

  • Step-3 - Print the result matrix.

  • Step-4 - In the replaceNum method, use a for loop to iterate over the rows and columns of the matrix.

  • Step-5 - For each element in the matrix, use the Math.signum method to determine the sign of the number (-1 for negative, 0 for 0, 1 for positive). Then use an if-else statement to replace the number with 0 or 1, depending on whether it is negative or positive

grammar

To get the length of an array (the number of elements in the array), the array has a built-in property, which is length

The following is its syntax -

array.length
Copy after login

Among them, "array" refers to the array reference.

The Math.signum() method in Java is a mathematical function that returns the sign of a given double or floating point value (-1 means negative, 0 means 0, 1 means positive) .

The following is its syntax -

Math.signum(mat[a][b]) == -1.0)
Copy after login

Among them, ‘mat’ refers to the given matrix.

Multiple methods

We provide solutions in different ways.

  • By using the ternary operator

  • By using the Math.signum function

Let’s look at the program and its output one by one.

Method 1: Use the ternary operator

In this method, the matrix elements will be initialized in the program. The user-defined method is then called by passing the matrix as an argument and uses the ternary operator to replace negative numbers with 0 and positive numbers with 1 according to the algorithm.

Example

public class Main {
   public static void main(String[] args) {
      int[][] inputMatrix = {{-1, 2, -3},
         {4, -5, 6},
         {7, 8, -9}};
      
      // Call the User-defined method to replace negative and positive numbers
      replacenum(inputMatrix);
      
      // Print the resultant matrix
      for (int a = 0; a < inputMatrix.length; a++) {
         for (int b = 0; b < inputMatrix[a].length; b++) {
            System.out.print(inputMatrix[a][b] + " ");
         }
         System.out.println();
      }
   }
   
   // User-defined method to replace numbers 
   public static void replacenum(int[][] mat) {
      for (int a = 0; a < mat.length; a++) {
         for (int b = 0; b < mat[a].length; b++) {
            
            // Use a ternary operator to replace the number
            mat[a][b] = mat[a][b] < 0 ? 0 : 1;
         }
      }
   }
}
Copy after login

Output

0 1 0 
1 0 1 
1 1 0
Copy after login

Method 2: Using the Math.signum function

In this method, the matrix elements will be initialized in the program. The user-defined method is then called by passing the matrix as a parameter and inside the method using the Math.signum method, negative numbers are replaced with 0 and positive numbers are replaced with 1 according to the algorithm.

Example

public class Main {
   public static void main(String[] args) {
      int[][] inputMatrix = {{-1, -2, 3},
         {4, -5, -6},
         {7, 8, 9}};
      
      // Call the User-defined method to replace negative and positive numbers
      replaceNum(inputMatrix);
      
      // Print the resultant matrix
      for (int a = 0; a < inputMatrix.length; a++) {
         for (int b = 0; b < inputMatrix[a].length; b++) {
            System.out.print(inputMatrix[a][b] + " ");
         }
         System.out.println();
      }
   }
   
   // User-defined method to replace numbers 
   public static void replaceNum(int[][] mat) {
      for (int a = 0; a < mat.length; a++) {
         for (int b = 0; b < mat[a].length; b++) {
            
            // Use an if-else statement with Math.signum method to replace the number
            if (Math.signum(mat[a][b]) == -1.0) {
               mat[a][b] = 0;
            } else {
               mat[a][b] = 1;
            }
         }
      }
   }
}
Copy after login

Output

0 0 1 
1 0 0 
1 1 1
Copy after login

In this article, we explored different ways to replace negative numbers with 0 and positive numbers with 1 in a matrix using the Java programming language.

The above is the detailed content of Replace negative and positive numbers in matrix with 0 and 1 in Java. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!