matlab da变换,将代码从MATLAB转换为R.(Converting code from MATLAB to R)

Matlab expression:

dA.*(A*N - N.*sum(A,2))

where

dA: real number

A: 10 x 10 matrix

N: 10 X 1 matrix

A*N: matrix multiplication

sum(A,2): sum of rows in A (10x1 matrix)

N.*sum(A,2): element by element multiplication (10 x 1 matrix)

Let's set up the following example in R:

A = matrix(data = 1:100,nrow = 10)

N = matrix(data = 1:10)

dA = 0.1

> A

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

[1,] 1 11 21 31 41 51 61 71 81 91

[2,] 2 12 22 32 42 52 62 72 82 92

[3,] 3 13 23 33 43 53 63 73 83 93

[4,] 4 14 24 34 44 54 64 74 84 94

[5,] 5 15 25 35 45 55 65 75 85 95

[6,] 6 16 26 36 46 56 66 76 86 96

[7,] 7 17 27 37 47 57 67 77 87 97

[8,] 8 18 28 38 48 58 68 78 88 98

[9,] 9 19 29 39 49 59 69 79 89 99

[10,] 10 20 30 40 50 60 70 80 90 100

> N

[,1]

[1,] 1

[2,] 2

[3,] 3

[4,] 4

[5,] 5

[6,] 6

[7,] 7

[8,] 8

[9,] 9

[10,] 10

The first term is:

z1 = A %*% N

And the second term:

srow = rowSums(A)

z2 = srow * N

Which leads to the final result:

result = dA * (z1-z2)

Final equation

result = dA * (A %*% N - rowSums(A)*N)

This should give you the same answer as Matlab's dA.*(A*N - N.*sum(A,2))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值