java - 给定计算的程序给出错误的输出
问题描述
任何人都可以帮助我检查为什么代码没有给我正确的答案或我做错了什么。我会感谢你的帮助。
此外,下面显示了我尝试实现的计算和代码。提前致谢。
扇形度量的计算如下所示
给定一个数据 Si = { 0.2554, 0.4464 , 0.3897 , 4.2112}
,例如迭代每个值
========================================================
Sj1 = 0.2554 Si1 = {0.2554}
Si1 > Sj1
0.2554 > 0.2554 = 0
Si1 = Sj2
0.2554 = 0.2554 = 1
Fand
(0 + (0.5 * 1) )/ 1 = 0.5
==========================================================
Sj2 = 4.4464 Si2 = {0.2554, 4.4464}
Si2 > Sj2
0.2554 > 4.4464 = 0
4.4464 > 4.4464 = 0
Si2 = Sj2
0.2554 = 4.4464 = 0
4.4464 = 4.4464 = 1
Fand
( 0 + (0.5 * 1) )/ 2 = 0.25
====================================================
Sj3 = 0.3897 Si3 = {0.2554, 4.4464, 0.3897}
Si3 > Sj3
0.2554 > 0.3897 = 0
4.4464> 0.3897 = 1
0.3897 > 0.3897 = 0
Si3 = Sj3
0.2554 = 0.2554 = 0
4.4464 = 0.3897 = 0
0.3897 = 0.3897 = 1
Fand
( 1 + (0.5 * 1) )/ 3 = 0.5 = 0.5
===========================================================
Sj4 = 4.2112 Si4 = {0.2554, 0.4464, 0.3897, 0.42112}
Si4 > Sj4
0.2554 > 4.2112 = 0
0.4464 > 4.2112 = 1
0.3897 > 4.2112 = 0
0.42112 > 4.2112 = 0
Si4 = Sj4
0.2554 = 4.2112 = 0
0.4464 = 4.2112 = 0
0.3897 = 4.2112 = 0
0.42112 = 4.2112 = 1
Fand
( 1 + (0.5 * 1) )/ 4 = 0.5 = 0.375
======================================================
Result
0.5, 0.25, 0.5, 0.375
The Fand formular in matlab code is
value = (sum( a(1:i) > a(i) ) + 0.5 * sum ( a(i) == a(1:i) ) ))/i
The Fand normal formular
Fand = ( #(si > sj) + 0.5 (si = sj)))/ i
======================================================
请在下面找到我的 Java 类 madion:
class madion {
public static void main(String[] args) {
double data[] = {0.2554, 4.4464, 0.3897, 4.2112};
//initialise variables
double sam = 0;
double sam1 = 0;
int i;
int j = 0;
double m = 0;
double n = 0;
int count = 0;
int count1 = 0;
//use for loop to iterate through the arrays
for (i = 0; i < data.length; i++) {
n = data[i];
for (j = 0; j <= i; j++) {
m = data[j];
count = 0;
//use if statement to express the conditions and counts
if (n == m) {
count++;
} else if (m > n) {
count1++;
}
}
double fand = 0;
for (i = 1; i <= data.length; i++) {
// System.out.println(i );
fand = ((count1) + (0.5 * count)) / i;
System.out.println(fand);
// System.out.println(count);
// System.out.println(count1);
}
}
}
}
我希望输出是,0.5, 0.25, 0.5, 0.375
但我实际上得到了
0.5, 0.25, 0.1666, 0.125
解决方案
Matlab 解决方案中存在三个不同的地方。
- 您除以的 i 在 Java 中从 0 开始,在 Matlab 中从 1 开始
- 那里有一个额外的循环来操纵 i 值
- 计数在不同级别上重置
代码应如下所示:
class madion {
public static void main(String[] args) {
double data[] = {0.2554, 4.4464, 0.3897, 4.2112};
//use for loop to iterate through the arrays
for (int i = 0; i < data.length; i++) {
double n = data[i];
int count = 0;
int count1 = 0;
for (int j = 0; j <= i; j++) {
double m = data[j];
//use if statement to express the conditions and counts
if (n == m) {
count++;
}
if (m > n) {
count1++;
}
}
double fand = ((count1) + (0.5 * count)) / (i+1);
System.out.println(fand);
}
}
}
推荐阅读
- vb.net - 如何从 vb.net 中的二进制文件中读取 vb6 定长字符串
- javascript - Vonage Video API 中未显示视频开/关按钮
- mysql - SQL 中是否有更好的 CASE 替代方案?
- ruby-on-rails - 不要从方法中收到任何消息
- python - 如何有效地矢量化(即避免显式循环)将某些颜色映射到 Numpy 数组中的其他颜色
- c# - c# Excel Interop 将字符串写入单元格
- python - SQLAlchemy 级联删除到多对多表
- react-native - 无法保持 react-native redux 状态
- solace - 支持 TLS 的 C 语言中的 Solace 客户端
- deep-learning - 如何在数据集上训练更快的rcnn,包括pytorch中的负数据