- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replace Negative and Positive Matrix Elements with 0 & 1 in Java
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type. The matrix in java is nothing but a multi-dimensional array which represents multiple rows and columns.
Here we have given a matrix which contains set of elements including both 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 deep dive into this article, to know how it can be done by using Java programming language.
To show you some instances
Instance-1
Given matrix =
-21 22 -23 24 -25 26 -27 -28 29
After replacing the negative numbers with 0 and positive numbers with 1,
The resultant matrix is −
0 1 0 1 0 1 0 0 1
Instance-2
Given matrix =
-9 2 -2 4 -1 -7 -2 6 2 -2 -4 3 -1 4 7 -8
After replacing the negative numbers with 0 and positive numbers with 1,
The resultant matrix is −
0 1 0 1 0 0 0 1 1 0 0 1 0 1 1 0
Instance-3
Given matrix =
-1 -2 -3 4 5 6 -7 8 -9
After replacing the negative numbers with 0 and positive numbers with 1,
The resultant matrix is: -
0 0 0 1 1 1 0 1 0
Algorithm
Algorithm-1
Step-1 − Create a 2D array matrix to store the numbers.
Step-2 − Call the replaceNum method to replace negative numbers with 0 and positive numbers with 1 in the matrix.
Step-3 − Print the resulting 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 a ternary operator to replace the number with 0 or 1, depending on whether it's negative or positive.
Algorithm-2
Step-1 − Create a 2D array matrix to store the numbers.
Step-2 − Call the replaceNum method to replace negative numbers with 0 and positive numbers with 1 in the matrix.
Step-3 − Print the resulting 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, and 1 for positive). Then use an if-else statement to replace the number with 0 or 1, depending on whether it's negative or positive
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length
Below refers to the syntax of it −
array.length
where, ‘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 float value (-1 for negative, 0 for 0, and 1 for positive).
Below refers to the syntax of it −
Math.signum(mat[a][b]) == -1.0)
Where, ‘mat’ refers to the given matrix.
Multiple Approaches
We have provided the solution in different approaches.
By Using Ternary Operator
By Using Math.signum Function
Let’s see the program along with its output one by one.
Approach-1: By Using Ternary Operator
In this approach, matrix elements will be initialized in the program. Then calls a user defined method by passing the matrix as parameter and inside method as per the algorithm using ternary operator replace negative numbers with 0 and positive numbers with 1.
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; } } } }
Output
0 1 0 1 0 1 1 1 0
Approach-2: By Using Math.signum Function
In this approach, matrix elements will be initialized in the program. Then calls a user defined method by passing the matrix as parameter and inside method as per the algorithm using Math.signum method replace negative numbers with 0 and positive numbers with 1.
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; } } } } }
Output
0 0 1 1 0 0 1 1 1
In this article, we explored different approaches to replace negative numbers with 0 and positive numbers with 1 in a matrix by using Java programming language.