Flip the matrix

user2961927

Hi everyone who loves while hates R:

Let's say you want to turn matrix M

      [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

to N

       [,1] [,2] [,3]
[1,]    3    2    1
[2,]    6    5    4
[3,]    9    8    7

All you need to do is

N<-M[,c(3:1)]

And N's structure is still a matrix

However, when you want to turn matrix M

       [,1] [,2] [,3]
[1,]    1    2     3  

to N

       [,1] [,2] [,3]
[1,]    3    2     1  

if you do N<-M[,c(3:1)] R will give you

N
[1] 3 2 1

N now is a vector! Not a matrix!

My solution is N<-M%*%diag(3)[,c(3:1)] which needs big space to store the identity matrix however.

Any better idea?

joran

You're looking for this:

N<-M[,c(3:1),drop = FALSE] 

Read ?Extract for more information. This is also a FAQ. This behavior is one of the most common debates folks have about the way things "should" be in R. My general impression is that many people agree that drop = FALSE might be a more sensible default, but that behavior is so old that changing it would be enormously disruptive to vast swaths of existing code.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to Vertically Flip an BitmapImage

来自分类Dev

Understanding Type of `flip ($)`

来自分类Dev

Libgdx-TextureRegion.flip()

来自分类Dev

Libgdx:for循环内的.Flip()

来自分类Dev

为什么在 Haskell 中 flip 和 flip(:) 是不同的函数?

来自分类Dev

Android Flip垂直动画XML

来自分类Dev

JavaScript中的`flip`函数实现?

来自分类Dev

CSS Flip Animation是否准时?

来自分类Dev

CSS Flip Animation类似Flipboard

来自分类Dev

Firefox中的CSS Flip动画问题

来自分类Dev

LibGDX Flip 2D Sprite动画

来自分类Dev

Python - Encoding file using flip cipher

来自分类Dev

coord_flip()混合了轴标签?

来自分类Dev

如何使用{flip:false}退出“ rtl”?

来自分类Dev

TypeError:game_1.flip不是函数

来自分类Dev

coord_flip()混合了轴标签?

来自分类Dev

JQM Flip Toggle Switch播放停止音频

来自分类Dev

BitSet flip()是否会影响BitSet的长度?

来自分类Dev

使用带或不带值的 array_flip

来自分类Dev

jQuery Flip on hover - using flag to keep flip from running repeatedly on hover

来自分类Dev

这个基于折叠的函数如何工作:“ myreverse =折叠(flip(:))[]”?

来自分类Dev

JQM later prepended flip switch doesn't react on events

来自分类Dev

jQuery Flip切换刷新而无需操作

来自分类Dev

api <3.0上的android Flip Card动画不向后翻转

来自分类Dev

R中coord_flip之后的颠倒顺序

来自分类Dev

Flip text upside down on the horizontal axis on hover - jquery or css

来自分类Dev

如何在Swift中编写一个flip方法?

来自分类Dev

Vanilla JavaScript Flip Card游戏无法正常工作

来自分类Dev

Vanilla JavaScript Flip Card游戏无法正常工作