
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Find Scalar Multiplication of a Matrix 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.
Scalar multiplication is nothing but multiplication between a matrix and a real number. The result of this multiplication is in matrix format.
As per the problem statement, we have to find the scalar multiplication of a matrix.
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
Suppose the original matrix is { {10, 20, 30}, {40, 50, 60}, {70, 80, 90} }
Scalar value: 5
Resultant Matrix − 50 100 150 200 250 300 350 400 450
Instance-2
Suppose the original matrix is { {10, 20, 30, 40}, {40, 50, 60, 20}, {70, 80, 90, 90} }
Scalar value: 12
Resultant Matrix − 120 240 360 480 600 720 840 960 1080
Instance-3
Suppose the original matrix is { {12, 123, 12131}, {11, 67, 896}, {233, 234, 678} }
Scalar value: 3
Resultant Matrix − 36 369 36393 33 201 2688 699 702 2034
Algorithm
Step 1 − Declare and initialize an integer type multi-dimensional array.
Step 2 − Declare another empty integer type multi-dimensional array to store the resultant matrix elements.
Step 3 − Initiate a nested for loop to multiply the scalar value with the given matrix elements and store those product values into that empty matrix.
Step 4 − Print the resultant matrix as output.
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.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Matrix
By Using User Defined Method
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Matrix
In this approach, array elements will be initialized in the program. Then as per the algorithm calculate the scalar multiplication.
Example
public class Main { public static void main(String[] args) { //declare and initialize an integer type multi- dimensional array int inputMatrix[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; //declare a variable to store scalar value int s = 2; //declare a variable to store the given matrix length value int len = inputMatrix.length; System.out.println("Given Matrix: "); for (int i = 0; i < len; i++){ for (int j = 0; j < len; j++){ System.out.print(inputMatrix[i][j] + " "); } System.out.println(); } System.out.println("The scalar value is: " + s); //declare an empty integer type multi- dimensional array int temp[][] = new int[len][len]; //initiate the loop to calculate the scalar Multiplication for (int i = 0; i < len; i++) { for (int j = 0; j < len; j++){ temp[i][j] = inputMatrix[i][j] * s; } } System.out.println("The scalar multiplication of given Matrix is: "); //initiate the loop to print the resultant matrix for (int i = 0; i < len; i++){ for (int j = 0; j < len; j++){ System.out.print(temp[i][j] + " "); } System.out.println(); } } }
Output
Given Matrix: 1 2 3 4 5 6 7 8 9 The scalar value is: 2 The scalar multiplication of given Matrix is: 2 4 6 8 10 12 14 16 18
Approach-2: By Using Dynamic Initialization of Matrix
In this approach, array elements will be initialized in the program. Then call a user defined method by passing this array as parameter. Inside the method as per the algorithm calculate the scalar multiplication of the matrix.
Example
public class Main { public static void main(String[] args) { //declare an integer type multi- dimensional array and add some random values int matrix[][] = {{11, 22, 34}, {42, 25, 61}, {72, 83, 94}}; //declare a variable to store scalar value int scalar = 10; //call the user-defined method scalarMultiplication(matrix,scalar); } //user-defined method to calculate the scalar Multiplication public static void scalarMultiplication(int[][] inputMatrix, int s) { //declare a variable to store the given matrix length value int len = inputMatrix.length; System.out.println("Given Matrix: "); for (int i = 0; i < len; i++){ for (int j = 0; j < len; j++){ System.out.print(inputMatrix[i][j] + " "); } System.out.println(); } System.out.println("The scalar value is: " + s); //declare an empty integer type multi- dimensional array int temp[][] = new int[len][len]; //initiate the loop to calculate the scalar Multiplication for (int i = 0; i < len; i++) { for (int j = 0; j < len; j++){ temp[i][j] = inputMatrix[i][j] * s; } } //output screen System.out.println("The scalar multiplication of given Matrix is: "); //initiate the loop to print the resultant matrix for (int i = 0; i < len; i++){ for (int j = 0; j <len; j++){ System.out.print(temp[i][j] + " "); } System.out.println(); } } }
Output
Given Matrix: 11 22 34 42 25 61 72 83 94 The scalar value is: 10 The scalar multiplication of given Matrix is: 110 220 340 420 250 610 720 830 940
In this article, we explored different approaches to find scalar multiplication of a matrix by using Java programming language.
- Related Articles
- Find Multiplication of Diagonal Elements of a Matrix in Java
- Scalar multiplication with Einstein summation convention in Python
- Matrix Chain Multiplication
- Matrix multiplication algorithm
- Sparse Matrix Multiplication in C++
- Python program multiplication of two matrix.
- Algorithm for matrix multiplication in JavaScript
- Program to check diagonal matrix and scalar matrix in C++
- Matrix Multiplication and Normalization in C program
- Matrix Chain Multiplication (A O(N^3) Solution) in C++
- Find Number of Sorted Rows in a Matrix in Java?
- Solve a linear matrix equation or system of linear scalar equations in Python
- C++ Program to Perform Matrix Multiplication
- C Program for Matrix Chain Multiplication
- Java Program to Find Transpose of a Matrix
