推广特定下三角矩阵的for-loop

时间:2016-12-28 12:13:49

标签: r for-loop

我有一个list个对象,比如

[a1 , a2, a3, a4, a5, b1, b2, b3, b4, c1, c2, c3, d1, d2, e1]

必须像

一样打印
a1
a2 b1
a3 b2 c1 
a4 b3 c2 d1
a5 b4 c3 d2 e1

使用2个for-loops?

1 个答案:

答案 0 :(得分:1)

如果初始数据是float y = recyclerView.getY() + recyclerView.getChildAt(selectedPosition).getY(); scrollView.smoothScrollTo(0, (int) y); (' v1'),我们可以根据频率创建vector(' m1')空白用字母替换下三角形(matrix)。

lower.tri

数据

n<- max(table(sub("\\d+", "", v1)))
m1 <- matrix("", ncol=n, nrow=n)
m1[lower.tri(m1, diag=TRUE)] <- v1
m1
#     [,1] [,2] [,3] [,4] [,5]
#[1,] "a1" ""   ""   ""   ""  
#[2,] "a2" "b1" ""   ""   ""  
#[3,] "a3" "b2" "c1" ""   ""  
#[4,] "a4" "b3" "c2" "d1" ""  
#[5,] "a5" "b4" "c3" "d2" "e1"