在下三维数组

时间:2017-12-05 13:03:47

标签: arrays r

A成为1 x 2 x 2 - 数组:

> A <- array(0, dim=c(1,2,2))
> A
, , 1

     [,1] [,2]
[1,]    0    0

, , 2

     [,1] [,2]
[1,]    0    0

然后A[,,1]是无量纲的:

> A[,,1]
[1] 0 0

我想:

     [,1] [,2]
[1,]    0    0

drop参数没有产生我想要的东西:

> A[,,1,drop=FALSE]
, , 1

     [,1] [,2]
[1,]    0    0

我发现这很烦人。并且有缺陷,因为R识别向量矩阵的向量,而不是行矩阵。

当然我可以做matrix(A[,,1], 1, 2)。有更方便的方式吗?

4 个答案:

答案 0 :(得分:2)

我们可以根据我们正在提取的import java.io.*; class WORD2 { public void main() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the sentence"); String str=br.readLine().toUpperCase()+" "; int l=str.length(); String word=""; int p=0; String word2[]=new String[l]; for(int i=0;i<str.length();i++) { char ch=str.charAt(i); **if((str.charAt(i))&&(str.charAt(i+1)!= ' '))**// THE ERROR COMES IN THIS STATEMNET SHOWING THE ERROR { if(ch!=' ') { word=word+ch; //TAKING EACH WORD } else { word2[p++]=word; //STORING EACH WORD word=""; } }} String q=""; for(int k=0;k<p;k++) { q=q+word2[k]+" "; } System.out.print("q="+q); }} 分配dim

MARGIN

使用另一个例子

`dim<-`(A[, ,1], apply(A, 3, dim)[,1])
 #     [,1] [,2]
 #[1,]    0    0

如果我们使用的是套餐解决方案,则来自B <- array(0, dim = c(2, 1, 2)) `dim<-`(B[, ,1], apply(B, 3, dim)[,1]) # [,1] #[1,] 0 #[2,] 0 的{​​{1}}可以获得预期的输出

adrop

答案 1 :(得分:1)

t(A[,,1])
 [,1] [,2]
[1,]    0    0

我从你的评论中可以看出,R将向量识别为列矩阵。我认为转置它可以提供你想要的东西,而且更方便。

答案 2 :(得分:1)

不幸的是,没有一个简单的方法可以解决这个问题。使用drop = TRUE时,1的任何维度都会被删除。如果您从src/main/subset.c查看ArraySubset的来源,就可以看到这一点。在函数结束时我们看到:

if (drop)
    DropDims(result);

DropDims函数在src/main/array.c中定义。在该功能中,我们看到了

n = 0;
for (i = 0; i < ndims; i++)
    if (dim[i] != 1) n++;

和函数中的其他地方我们看到尺寸为1的尺寸会被忽略。

令人讨厌的是R确定了结果中的丢弃行为。如果R只删除长度为1的索引维度,那将更自然,因此行为将如您在此示例中所预期的那样。

所有这些,说,以下是丑陋的,它可能适用于您的用例的错误:

`dim<-`(A[,,1], dim(A)[-3])
 #>      [,1] [,2]
 #> [1,]    0    0

这并不能很好地概括,但是如果你用单个坐标进行索引就可以了。

答案 3 :(得分:0)

另一个包解决方案:keep包。

> library(keep)
> A <- karray(0, dim=c(1,2,2))
> A[,,1]
     [,1] [,2]
[1,]    0    0
attr(,"class")
[1] "keep"    "numeric"