Convert JAVA code to MATLAB code
1 次查看(过去 30 天)
显示 更早的评论
Can any one convert the java code (see the attachments) to MATLAB code (m file)
3 个评论
Futun Salem fayez alharbi
2022-10-30
int maxProductSubset(int a[], int n) { if (n == 1) return a[0];
// Find count of negative numbers, count
// of zeros, negative number
// with least absolute value
// and product of non-zero numbers
int max_neg = INT_MIN;
int count_neg = 0, count_zero = 0;
int prod = 1;
for (int i = 0; i < n; i++) {
// If number is 0, we don't
// multiply it with product.
if (a[i] == 0) {
count_zero++;
continue;
}
// Count negatives and keep
// track of negative number
// with least absolute value
if (a[i] < 0) {
count_neg++;
max_neg = max(max_neg, a[i]);
}
prod = prod * a[i];
}
// If there are all zeros
if (count_zero == n)
return 0;
// If there are odd number of
// negative numbers
if (count_neg & 1) {
// Exceptional case: There is only
// negative and all other are zeros
if (count_neg == 1 &&
count_zero > 0 &&
count_zero + count_neg == n)
return 0;
// Otherwise result is product of
// all non-zeros divided by
//negative number with
// least absolute value
prod = prod / max_neg;
}
return prod;
}
// Driver Code int main() { int a[] = { -1, -1, -2, 4, 3 }; int n = sizeof(a) / sizeof(a[0]); cout << maxProductSubset(a, n); return 0; }
回答(2 个)
praneeth ranga
2017-9-3
编辑:Walter Roberson
2017-9-4
package kcjava;
import org.apache.commons.math3.distribution.NormalDistribution;
import org.graphstream.graph.Edge;
import org.graphstream.graph.Element;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.SingleGraph;
import java.util.*;
class GraphOpers{
LinkedList<String> nonLeafNodes(LinkedList<GraphData> ld){
LinkedList<String> al = new LinkedList<String>();
for(int i=0;i<ld.size();i++){
for(int j=0;j<ld.size();j++){
for(int k=0;k+1<=ld.get(j).pred.length();k=k+2){
if(ld.get(i).act.equals(ld.get(j).pred.substring(k,k+1)) && !al.contains(ld.get(i).act) && !ld.get(j).pred.equals("NULL")){
al.add(ld.get(i).act);
break;
}
}
}
}
return al;
}
GraphData findNode(LinkedList<GraphData> ld,String ss){
for(int i=0;i<ld.size();i++){
if(ld.get(i).act.equals(ss)){
return ld.get(i);
}
}
return null;
}
int sizeofGraph(LinkedList<GraphData> ld,int n){
LinkedList<String> al = new LinkedList<String>();
for(int i=0;i<ld.size();i++){
GraphData dd = ld.get(i);
if(dd.pred.length()>1 && !al.contains(dd.pred) && !dd.pred.equals("NULL")){
al.addLast(dd.pred);
n=(int) (n-Math.floor(dd.pred.length()/2));
}
}
return n;
}
int[][] makeEdge(int g[][],int i,int j){
g[i][j]=1;
return g;
}
int fact(int n){
int j=1;
for(int i=2;i<=n;i++){
j=j*i;
}
return j;
}
float add(float z){
float res=z,temp=z;
for(int i=1;i<15;i++){
temp=temp*z*z/(2*i+1);
res=res+temp;
}
return res;
}
float erf(float z){
float pi = (float) 3.1415926,res=0;
for(int i=0;i<25;i++){
res=(float) (res+(Math.pow(-1,i)*Math.pow(z,2*i+1))/(fact(i)*(2*i+1)));
}
res=(float) (res*2/Math.sqrt(pi));
return res;
}
}
class GraphData{
String act;
String pred;
float period;
float sd;
int start;
int end;
GraphData(String act,String pred,float period,float sd,int start,int end){
this.act=act;
this.pred=pred;
this.sd=sd;
this.period=period;
this.start=start;
this.end=end;
}
}
class GraphNode{
float es;
float lc;
GraphNode(float es,float lc){
this.es=es;
this.lc=lc;
}
}
public class CPM {
public static void main(String[] args) {
LinkedList<GraphData> ld = new LinkedList<GraphData>();
LinkedList<GraphNode> ln = new LinkedList<GraphNode>();
LinkedList<Integer> lres = new LinkedList<Integer>();
LinkedList<Integer> tf = new LinkedList<Integer>();
LinkedList<Integer> ff = new LinkedList<Integer>();
LinkedList<String> al = new LinkedList<String>();
LinkedList<String> lc = new LinkedList<String>();
LinkedList<String> al1 = new LinkedList<String>();
Scanner sc = new Scanner(System.in);
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
GraphOpers go = new GraphOpers();
int n,sog,cou=2;
float dt,v=0;
System.out.println("Enter number of activities:");
n= sc.nextInt();
System.out.println("Enter tm, tp, to,activity, predecessor:");
for(int i=0;i<n;i++){
float tm,tp,to;
tm=sc.nextFloat();to=sc.nextFloat();tp=sc.nextFloat();
float j=(to+4*tm+tp)/6;
float sd=(tp-to)/6;
GraphData d= new GraphData(sc.next(),sc.next(),j,sd,0,0);
ld.add(d);
}
al=go.nonLeafNodes(ld);
sog=go.sizeofGraph(ld,n)+2-n+al.size();
for(int i=0;i<ld.size();i++){
if(ld.get(i).pred.equals("NULL") && !ld.get(i).act.equals("DU")){
ld.get(i).start=1;
ld.get(i).end=cou;
cou++;
}
else if(!ld.get(i).pred.equals("NULL") && al.contains(ld.get(i).act)){
for(int j=0;j<ld.size();j++){
for(int k=0;k+1<=ld.get(j).pred.length();k=k+2){
GraphData gd1,gd2;
if(ld.get(j).pred.length()>1 && !al1.contains(ld.get(i).act) && ld.get(i).act.equals(ld.get(j).pred.substring(k,k+1)) && !ld.get(j).pred.equals("NULL")){
if(k-2>=0){
al1.add(ld.get(i).act);
gd1=go.findNode(ld,ld.get(i).pred.substring(0,1));
gd2=go.findNode(ld,ld.get(j).pred.substring(k-2,k-1));
ld.get(i).start=gd1.end;
ld.get(i).end=gd2.end;
for(int kk=0;kk<i;kk++){
if(ld.get(i).start==ld.get(kk).start && ld.get(i).end==ld.get(kk).end && i!=kk){
sog++;
ld.get(i).end=cou;
GraphData d= new GraphData("DU","NULL",0,0,cou,ld.get(kk).end);
cou++;
ld.add(d);
}
}
break;
}
else{
gd1=go.findNode(ld,ld.get(i).pred.substring(0,1));
al1.add(ld.get(i).act);
ld.get(i).start=gd1.end;
ld.get(i).end=cou;
cou++;
/*for(int kk=0;kk<i;kk++){
if(ld.get(i).start==ld.get(kk).start && ld.get(i).end==ld.get(kk).end && i!=kk){
sog++;
ld.get(i).end=cou;
cou++;
GraphData d= new GraphData("DU","NULL",0,0,cou,ld.get(kk).end);
ld.add(d);
}
}*/
break;
}
}
else if(ld.get(j).pred.length()<=1 && !al1.contains(ld.get(i).act) && ld.get(i).act.equals(ld.get(j).pred.substring(k,k+1)) && !ld.get(j).pred.equals("NULL")){
gd1=go.findNode(ld,ld.get(i).pred.substring(0,1));
al1.add(ld.get(i).act);
ld.get(i).start=gd1.end;
ld.get(i).end=cou;
cou++;
/* for(int kk=0;kk<i;kk++){
if(ld.get(i).start==ld.get(kk).start && ld.get(i).end==ld.get(kk).end && i!=kk){
sog++;
cou++;
ld.get(i).end=cou;
GraphData d= new GraphData("DU","NULL",0,0,cou,ld.get(kk).end);
ld.add(d);
}
}*/
break;
}
}
}
}
else if(!ld.get(i).pred.equals("NULL") && !al.contains(ld.get(i))){
GraphData gd =go.findNode(ld,ld.get(i).pred.substring(0,1));
ld.get(i).start=gd.end;
ld.get(i).end=cou;
}
}
for(int i=0;i<sog;i++){
GraphNode gn = new GraphNode(0,0);
ln.add(gn);
}
/*for(int i=0;i<ld.size();i++){
for(int j=0;j<ld.size();j++){
if(ld.get(i).start==ld.get(j).start && ld.get(i).end==ld.get(j).end && i!=j){
GraphNode gn = new GraphNode(0,0);
ln.add(gn);
cou++;
ld.get(j).end=cou;
GraphData d= new GraphData("DU","NULL",0,0,cou,ld.get(i).end);
ld.add(d);
}
}
}*/
for(int i=0;i<ld.size();i++){
System.out.println(ld.get(i).act+":("+ld.get(i).start+","+ld.get(i).end+")");
}
for(int i=0;i<ld.size();i++){
if(ld.get(i).pred.equals("NULL") && !ld.get(i).act.equals("DU")){
ln.get(ld.get(i).end-1).es=ld.get(i).period;
}
if(ln.get(ld.get(i).end-1).es==0){
ln.get(ld.get(i).end-1).es=ln.get(ld.get(i).start-1).es+ld.get(i).period;
}
if(ln.get(ld.get(i).end-1).es>0 && ln.get(ld.get(i).end-1).es<ln.get(ld.get(i).start-1).es+ld.get(i).period){
ln.get(ld.get(i).end-1).es=ln.get(ld.get(i).start-1).es+ld.get(i).period;
}
}
ln.get(sog-1).lc=ln.get(sog-1).es;
for(int i=ld.size()-1;i>=0;i--){
if(ln.get(ld.get(i).start-1).lc==0){
ln.get(ld.get(i).start-1).lc=ln.get(ld.get(i).end-1).lc-ld.get(i).period;
}
if(ln.get(ld.get(i).start-1).lc>ln.get(ld.get(i).end-1).lc-ld.get(i).period){
ln.get(ld.get(i).start-1).lc=ln.get(ld.get(i).end-1).lc-ld.get(i).period;
}
}
ln.get(0).lc=0;
for(int i=0;i<ln.size();i++){
if(ln.get(i).es==ln.get(i).lc){
lres.add(i+1);
}
}
System.out.print("Critical Path: ");
for(int i=0;i<ld.size();i++){
for(int j=0;j<lres.size()-1;j++){
//System.out.println(lres.get(j)+" "+ld.get(i).start+" "+lres.get(j+1)+" "+ld.get(i).end);
if(lres.get(j)==ld.get(i).start && lres.get(j+1)==ld.get(i).end){
System.out.print(ld.get(i).act+" ");
lc.add(ld.get(i).act);
v=v+ld.get(i).sd*ld.get(i).sd;
}
}
}
System.out.println();
System.out.println("Variance:"+v);
v=(float)Math.sqrt(v);
for(int i=0;i<ld.size();i++){
if(ld.get(i).start>=1){
tf.add((int) (ln.get(ld.get(i).end-1).lc-ln.get(ld.get(i).start-1).es-ld.get(i).period));
ff.add((int) (ln.get(ld.get(i).end-1).es-ln.get(ld.get(i).start-1).es-ld.get(i).period));
}
}
for(int i=0;i<ld.size();i++){
if(tf.get(i)==0 && ff.get(i)==0){
lres.add(i+1);
}
}
System.out.println("Enter Desired Time:");
dt=sc.nextFloat();
float ct=ln.get(ln.size()-1).es;
NormalDistribution nd=new NormalDistribution();
float z1=(dt-ct)/v;
System.out.println("Z:"+z1);
float z=go.erf((float) (z1/1.414));
z=(z+1)/2;
System.out.println("Probability:"+z);
}
}
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call C++ from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!