在r建立马尔可夫链(Building markov chain in r)
我在列中有一个文本,我想建立一个马尔可夫链。 我想知道有一种方法可以为状态A,B,C,D构建马尔可夫链,并生成具有该状态的马尔可夫链。 有什么想法吗?
A<- c('A-B-C-D', 'A-B-C-A', 'A-B-A-B')
I have a text in a column and i would like to build a markov chain. I was wondering of there is a way to build markov chain for states A, B,C, D and generate a markov chain with that states. Any thoughts?
A<- c('A-B-C-D', 'A-B-C-A', 'A-B-A-B')
原文:https://stackoverflow.com/questions/41409154
最满意答案
此代码检查
intOne
是否为null
,并且一检测到两个intOne
的任何一个为null
,就跳转到比较intTwo
:if(obj1.getIntOne()==null || obj2.getIntOne()==null){ return obj1.getIntTwo().compareTo(obj2.getIntTwo()); }
这会产生你看到的效果。 如果您希望获得您描述的效果,请按如下所示更改代码:
if(obj1.getIntOne()==null && obj2.getIntOne()==null) { return obj1.getIntTwo().compareTo(obj2.getIntTwo()); } else if(obj1.getIntOne()==null && obj2.getIntOne()!=null) { // Null is greater than any non-null number return 1; } else if(obj1.getIntOne()!=null && obj2.getIntOne()==null) { // Non-null is smaller than null return -1; } else { return obj1.getIntOne().compareTo(obj2.getIntOne()); }
仅当两个
intOne
都为null
时,此更改才使用intTwo
。 如果只有其中一个为null
,则null
的值被解释为一个非常大的数字。This code checks
intOne
fornull
, and jumps to comparingintTwo
s as soon as any of the twointOne
s is detected to benull
:if(obj1.getIntOne()==null || obj2.getIntOne()==null){ return obj1.getIntTwo().compareTo(obj2.getIntTwo()); }
This produces the effect that you see. If you would like to have the effect that you describe, change the code as follows:
if(obj1.getIntOne()==null && obj2.getIntOne()==null) { return obj1.getIntTwo().compareTo(obj2.getIntTwo()); } else if(obj1.getIntOne()==null && obj2.getIntOne()!=null) { // Null is greater than any non-null number return 1; } else if(obj1.getIntOne()!=null && obj2.getIntOne()==null) { // Non-null is smaller than null return -1; } else { return obj1.getIntOne().compareTo(obj2.getIntOne()); }
This change uses
intTwo
s only when bothintOne
s arenull
. If only one of them isnull
, the value ofnull
is interpreted as a very large number.
相关问答
更多-
根据文档 ,您还应该返回负值: 返回负整数,零或正整数,因为此对象小于,等于或大于指定对象。 public int compareTo(final Book theOther) { int result = myTitle.compareTo(theOther.myTitle); if (result == 0) { result = myAuthors.get(0).compareTo(theOther.myAuthors.get(0)); } return ...
-
也许令人困惑的是,像List这样的标准类不在java.lang甚至java.collections而是java.util 你需要Ljava/util/List Perhaps confusingly, standard classes like List are not in java.lang or even java.collections but java.util You need Ljava/util/List
-
具有自定义比较器的Collections.sort实际上并不排序(Collections.sort with Custom Comparator does not actually sort)[2023-06-29]
不要忘记break switch ( x ) { case 0 : Collections.sort( p.members, new compareThings.CEmpathy()); break; // IMPORTANT case 1 : Collections.sort( p.members, new compareThings.CFear()); break; // IMPORTANT case 2 : ... -
Collection.sort(l)假定Collection.sort(l)的内容是Comparable 。 Collection.sort(1, Comparator)使用自定义比较器来比较l的内容,这就是你所做的。 排序的思路(包括sort()方法)意味着对象必须具有可比性 - 在这种情况下,可以使用Comparable或Comparator 。 请注意,许多Java对象已经可以比较,包括String , Date和Number 。 对于那些,你可以使用Collection.sort(someList) ...
-
一个使用java-8特性完成任务的解决方案。 ArrayList
myList = new ArrayList<>(); Collections.sort(myList, Comparator.comparingDouble(Hw4b::getrNumber).reversed()); 这应该按照rNumber降序排序。 one solution to accomplishing your task using some of the java-8 features. ArrayList < ... -
您首先使用自定义比较器对列表进行排序。 然后,您将根据元素的反向自然排序重新排序 - 而不是您已经应用的自定义排序。 所以第一种排序没有生效,因为列表是按第二种排序重新排序的。 请注意, Collections.reverseOrder()不会颠倒列表 - 它与自然排序相反(所以getEtatList()的元素必须已经是Comparable )。 尝试失去第二类,并做: c.getEtatList().sort(new Comparator
() { @Override publi ... -
此代码检查intOne是否为null ,并且一检测到两个intOne的任何一个为null ,就跳转到比较intTwo : if(obj1.getIntOne()==null || obj2.getIntOne()==null){ return obj1.getIntTwo().compareTo(obj2.getIntTwo()); } 这会产生你看到的效果。 如果您希望获得您描述的效果,请按如下所示更改代码: if(obj1.getIntOne()==null && obj2.getIntOne ...
-
问题是你的比较器坏了。 当你有一个比较器 comparator.compare(a, b) == -comparator.compare(b, a) Java 7不接受这个的原因是; Java 7有更多检查,这种情况是正确的。 他们已将java更新为1.7 :(此代码现在不在那里工作:( 它从来没有工作过,它可能之前没有正确排序,但你之前没有遇到运行时错误。 一个较短的版本将起作用; (不要重用常用内置类的名称) static class ObjectSortComparator implements C ...
-
如果这是Java 7或更高版本,则使用TimSort。 TimSort首先运行输入并检测或收集32个或更多元素的升序运行(在此实现中)。 请参阅源代码中的countRunAndMakeAscending 。 目前,超过32的运行留在原地。 通过对当前运行中的后续元素进行二进制插入排序来延长短于32的运行,直到它至少为32个元素长。 请参阅源代码中的binarySort 。 (合并排序方法仅在收集> = 32的运行之后才完成。由于您的输入只有3个元素,因此整个排序是使用二进制插入排序完成的,并且不进行合并。) ...
-
Collections.sort()适用于Comparable或Comparator?(Collections.sort() works with Comparable or Comparator?)[2020-12-27]
Collections.sort(listOfInts, Integer::compare); 相当于旧Java中的这段代码: Collections.sort(list, new Comparator() { @Override public int compare(Integer o1, Integer o2) { return Integer.compare(o1, o2); } }); 而 Collections.sort(listOfI ...