matlab是一个强大的数学软件,其中实现了好多的数学函数,我们使用时直接调用即可。但java中有些方法需要自己去实现。
首先来看一下matlab中一些函数。
1.sign函数
A = [1,-2,6,0];
B = sign(A);
运行结果是
2.两个数组相加
A = [1,2,3,4];
B= [4,5,6,7];
C = A+B;
运行结果是
3.获取一个数组当中一段区间元素
A = [1,2,3,4,5,6,7];
C = A(2:5);
运行结果:
4.数组元素和
A = [1,2,3,4];
B = [2,3,4,5];
C = A+B;
运行结果:
5.两个数组相减
A = [1,2,3,4];
B = [2,3,4,5];
C = B-A;
运行结果:
6.一个数减一个数组,生成新的数组
A = [1,2,3,4];
C = 5-A;
运行结果:
7.一个数组每个元素乘以一个数
A = [1,2,3,4];
C = 5.*A;
运行结果:
8.一个数除以一个数组
A = [1,2,3,4];
C = 5./A;
运行结果:
9.数组平方和
A = [1,2,3,4];
C = sum(A.^2);
运行结果:
10.获取一个数组中最小值
A = [1,2,3,4];
C = min(A);
运行结果:
11.获取一个数组中最小值下标
A = [1,-1,3,4];
[C,index] = min(A);
运行结果:
12.生成一个数组
t = 0:0.2:1;
运行结果:
13.初始化数组
A = zeros(1,5);
运行结果:
14.数组平均值
A=[2,3,5,6];
b= mean(A);
运行结果:
15.产生一个正态分布的随机数
A = randn(5);
运行结果:
16.连接数组,生成一个新的数组
A = [1,2,3,4,5];
B = zeros(1,5);
C = [B,A,B];
运行结果:
java实现工具类
/**
* matlab中有些函数的工具类
*/
public class MathUtils {
/**
* 对应matlab中sign
* @param num
* @return
*/
public static int sign(float num){
return num == 0?0:(num > 0?1:-1);
}
/**
* 对应matlab中sign
* @param num
* @return
*/
public static int sign(double num){
return num == 0?0:(num > 0?1:-1);
}
/**
* 对应matlab中sign
* @param num
* @return
*/
public static int sign(int num){
return num == 0?0:(num > 0?1:-1);
}
/**
* 对应matlab中sign
* @param array
* @return
*/
public static int[] sign(int[] array){
int[] result = new int[array.length];
for (int i = 0;i < array.length;i++) {
result[i] = array[i] == 0?0:(array[i] > 0?1:-1);
}
return result;
}
/**
* 对应matlab中sign
* @param array
* @return
*/
public static double[] sign(double[] array){
double[] result = new double[array.length];
for (int i = 0;i < array.length;i++) {
result[i] = array[i] == 0?0:(array[i] > 0?1:-1);
}
return result;
}
/**
* 两个数组相加
* @param array1
* @param array2
* @return
*/
public static int[] arraySum(int[] array1,int[] array2){
int[] result = new int[array1.length > array2.length?array1.length:array2.length];
for (int i = 0;i < result.length;i++){
if (i < array1.length && i < array2.length){
result[i] = array1[i] + array2[i];
}else {
result[i] = i < array1.length?array1[i]:array2[i];
}
}
return result;
}
/**
* 两个数组相加
* @param array1
* @param array2
* @return
*/
public static double[] arraySum(double[] array1,double[] array2){
double[] result = new double[array1.length > array2.length?array1.length:array2.length];
for (int i = 0;i < result.length;i++){
if (i < array1.length && i < array2.length){
result[i] = array1[i] + array2[i];
}else {
result[i] = i < array1.length?array1[i]:array2[i];
}
}
return result;
}
/**
* 两个数组相加
* @param array1
* @param array2
* @return
*/
public static float[] arraySum(float[] array1,float[] array2){
float[] result = new float[array1.length > array2.length?array1.length:array2.length];
for (int i = 0;i < result.length;i++){
if (i < array1.length && i < array2.length){
result[i] = array1[i] + array2[i];
}else {
result[i] = i < array1.length?array1[i]:array2[i];
}
}
return result;
}
/**
* 数组一定区间元素和
* @param array 目标数组
* @param start 开始位置
* @param end 结束位置
* @return
*/
public static double arraySingleSumRange(double[] array,int start,int end){
double result = 0;
for (int i = start;i < end;i++){
result += array[i];
}
return result;
}
/**
* 数组元素和
* @param array
* @return
*/
public static double arraySingleSum(double[] array){
double result = 0;
for (int i = 0;i < array.length;i++){
result += array[i];
}
return result;
}
/**
* 数组元素和
* @param array
* @return
*/
public static int arraySingleSum(int[] array){
int result = 0;
for (int i = 0;i < array.length;i++){
result += array[i];
}
return result;
}
/**
* 两个数组相减
* @param array1
* @param array2
* @return array1-array2
*/
public static int[] arraySub(int[] array1,int[] array2){
int[] result = new int[array1.length > array2.length?array1.length:array2.length];
for (int i = 0;i < result.length;i++){
if (i < array1.length && i < array2.length){
result[i] = array1[i] - array2[i];
}else {
result[i] = i < array1.length?array1[i]:-array2[i];
}
}
return result;
}
/**
* 两个数组相减
* @param array1
* @param array2
* @return array1-array2
*/
public static double[] arraySub(double[] array1,double[] array2){
double[] result = new double[array1.length > array2.length?array1.length:array2.length];
for (int i = 0;i < result.length;i++){
if (i < array1.length && i < array2.length){
result[i] = array1[i] - array2[i];
}else {
result[i] = i < array1.length?array1[i]:-array2[i];
}
}
return result;
}
/**
* 两个数组相减
* @param num
* @param array
* @return num-array2
*/
public static double[] arraySub(int num,double[] array){
double[] result = new double[array.length];
for (int i = 0;i < array.length;i++){
result[i] = num-array[i];
}
return result;
}
/**
* 两个数组相减
* @param num
* @param array
* @return num-array2
*/
public static double[] arraySubAbs(double num,double[] array){
double[] result = new double[array.length];
for (int i = 0;i < array.length;i++){
result[i] = Math.abs(num-array[i]);
}
return result;
}
/**
* 单个数组绝对值
* @param array
* @return array
*/
public static double[] arraySingleAbs(double[] array){
double[] result = new double[array.length];
for (int i = 0;i < array.length;i++){
result[i] = Math.abs(array[i]);
}
return result;
}
/**
* 数组乘法后求和
* @param array1
* @param array2
* @return
*/
public static double arrayMultipleSum(double[] array1,double[] array2){
double result = 0;
if (array1.length != array2.length){
return result;
}
for (int i = 0;i < array1.length;i++){
result += array1[i]*array2[i];
}
return result;
}
/**
* 数组乘法
* @param num
* @param array
* @return
*/
public static float[] arrayMultiple(float[] array,int num){
float[] result = new float[array.length];
for (int i = 0;i < array.length;i++){
result[i] = array[i]*num;
}
return result;
}
/**
* 数组乘法
* @param num
* @param array
* @return
*/
public static double[] arrayMultiple(double[] array,double num){
double[] result = new double[array.length];
for (int i = 0;i < array.length;i++){
result[i] = array[i]*num;
}
return result;
}
/**
* 数组乘法
* @param num
* @param array
* @return
*/
public static float[] arrayMultiple(float[] array,double num){
float[] result = new float[array.length];
for (int i = 0;i < array.length;i++){
result[i] = (float) (array[i]*num);
}
return result;
}
/**
* 数组乘法
* @param num
* @param array
* @return
*/
public static int[] arrayMultiple(int[] array,int num){
int[] result = new int[array.length];
for (int i = 0;i < array.length;i++){
result[i] = array[i]*num;
}
return result;
}
/**
* 数组除法
* @param num
* @param array
* @return
*/
public static double[] arrayDivide(int num,int[] array){
double[] result = new double[array.length];
for (int i = 0;i < array.length;i++){
result[i] = num*1.0/array[i];
}
return result;
}
/**
* 数组除法
* 这个方法是如果array中存储的是别的数组下标,matlab中数组是以1为起始点的,而java中是以0开始
* @param num
* @param array
* @return
*/
public static double[] arrayDividePos(int num,int[] array){
double[] result = new double[array.length];
for (int i = 0;i < array.length;i++){
result[i] = num*1.0/(array[i]+1);
}
return result;
}
/**
* 一个数组平方和
* @param array
* @return array1-array2
*/
public static double arraySquareSum(double[] array){
double result = 0;
for (int i = 0;i < array.length;i++){
result+=array[i]*array[i];
}
return result;
}
/**
* 一个数组中的最小值
* @param array
* @return
*/
public static int arrayMin(int[] array){
int result = array[0];
for (int num:array) {
if (num < result){
result = num;
}
}
return result;
}
/**
* 一个数组中的最小值
* @param array
* @return
*/
public static double arrayMin(double[] array){
double result = array[0];
for (double num:array) {
if (num < result){
result = num;
}
}
return result;
}
/**
* 一个数组中的最小值的位置
* @param array
* @return
*/
public static int arrayIndexMin(int[] array){
int index = 0;
int result = array[0];
for (int i= 0;i