如何按照行标准差分行分割矩阵?
要按照行标准差分行分割 R 中的矩阵值,我们可以按以下步骤操作:
- 首先,创建矩阵。
- 然后,使用 apply 函数按行标准差分行分割矩阵值。
创建矩阵
我们来创建一个矩阵,如下图所示:
M<-matrix(sample(1:1000,100),ncol=4) M
在执行时,上述脚本会生成以下输出(此输出因随机化而在您的系统上有所不同):
[,1] [,2] [,3] [,4] [1,] 651 787 927 842 [2,] 9 633 698 445 [3,] 661 536 794 680 [4,] 910 396 814 91 [5,] 716 838 253 239 [6,] 227 547 754 371 [7,] 122 84 236 745 [8,] 992 969 348 523 [9,] 281 623 209 957 [10,] 415 453 578 827 [11,] 452 225 294 419 [12,] 796 675 321 41 [13,] 614 213 473 526 [14,] 392 447 652 50 [15,] 946 460 561 724 [16,] 362 108 571 594 [17,] 898 829 535 318 [18,] 994 951 988 938 [19,] 270 45 591 74 [20,] 840 5 154 470 [21,] 329 820 481 85 [22,] 297 906 265 932 [23,] 471 282 256 436 [24,] 434 425 13 36 [25,] 627 200 815 784
按照行标准差分行分割矩阵值
使用 apply 函数按行标准差分割 M 的行值:
M<-matrix(sample(1:1000,100),ncol=4) M_new<-t(apply(M,1, function(x) x/sd(x))) M_new
输出
[,1] [,2] [,3] [,4] [1,] 5.62023697 6.79435714 8.00301025 7.2691851 [2,] 0.02897489 2.03790061 2.24716371 1.4326473 [3,] 6.25585513 5.07282655 7.51459754 6.4356755 [4,] 2.39345882 1.04154911 2.14096207 0.2393459 [5,] 2.30488157 2.69761279 0.81443441 0.7693669 [6,] 0.99755809 2.40380738 3.31347488 1.6303703 [7,] 0.39904046 0.27474917 0.77191433 2.4367635 [8,] 3.07309809 3.00184683 1.07806264 1.6201918 [9,] 0.81641559 1.81006019 0.60722726 2.7804616 [10,] 2.23089532 2.43517007 3.10712650 4.4456637 [11,] 4.25199561 2.11659074 2.76567856 3.9415623 [12,] 2.31727586 1.96502664 0.93447934 0.1193572 [13,] 3.56077176 1.23525144 2.74307010 3.0504331 [14,] 1.56839005 1.78844478 2.60864876 0.2000498 [15,] 4.45878085 2.16811754 2.64416074 3.4124285 [16,] 1.60150472 0.47779699 2.52613037 2.6278834 [17,] 3.33974413 3.08312682 1.98971393 1.1826711 [18,] 36.17736050 34.61234390 35.95898609 34.1391993 [19,] 1.07405332 0.17900889 2.35098337 0.2943702 [20,] 2.27013002 0.01351268 0.41619050 1.2701918 [21,] 1.06944378 2.66548298 1.56353331 0.2763001 [22,] 0.80545896 2.45705663 0.71867550 2.5275682 [23,] 4.36118901 2.61115775 2.37041271 4.0371091 [24,] 1.85435094 1.81589666 0.05554507 0.1538171 [25,] 2.21382535 0.70616439 2.87761987 2.7681644
广告