我想创建一个2x3矩阵.(2行,3列)当我运行我的代码时,我在括号中得到矩阵,这是不正确的.
def fill_matrix(numrows, numcols, val):
matrix = [[val for i in range(numrows)] for j in range(numcols)]
return (numrows, numcols, val)
Run Code Online (Sandbox Code Playgroud)
如果我选择创建一个2x2矩阵并用1填充所有漏洞,我应该得到这个:[[1,1],[1,1]]
但我得到了这个:(2,2,1)
我想测试[[5,6],[7,8]]的2x2矩阵,看看它是否是正方形。
我运行我的代码,我应该得到True,但是我得到False ...
def square(sq):
for element in sq:
if element:
return False
return True
Run Code Online (Sandbox Code Playgroud) 我是Erlang的新手,所以这个问题可能有点基础.
我得到了与N ^ 2个元件(我知道N),我要分解成一个阵列(或元组,无所谓)用N元组,每个具有N个元素的数组.
即:我想转:
[14,20,26,20,29,38,26,38,50]
成
:[{14,20,26},{20,29,38},{26,38,50}}
是否有一种简单的方法或功能可以做到这一点?
谢谢.
我正在尝试编写一些代码来规范化带有元素的向量,[x,y,z]但是想知道是否有一种方法来规范化元素,这样每次元素的总和都会增加到1.
我确实遇到过一个公式:(伪代码)
normalized = vectorA/ magnitude(vectorA)
Run Code Online (Sandbox Code Playgroud)
但是,如果vectorA = [1,4,5]那时使用上面的公式会给出:
magnitude = sqrt(1^2 + 4^2 + 5^2)
magnitude = sqrt(42)
magnitude = 6.48
Run Code Online (Sandbox Code Playgroud)
现在基于那个,如果我将矢量的每个元素除以6.48:
1/6.48 = 0.154
4/6.48 = 0.62
5/6.48 = 0.77
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,总和大于1,因此公式不是每次都有效,我想知道是否有人知道如何在任何时候将元素总和等于1.
任何帮助将不胜感激
蟒蛇
如何转换网格:
x,x
x,x
Run Code Online (Sandbox Code Playgroud)
到列表列表?:
[['x', 'x'], ['x', 'x']]
Run Code Online (Sandbox Code Playgroud) 我有代码直接改变矩阵的性能.在我改变它之前,我希望得到一个完整的副本来存储一个新符号,然后由变异过程使用.无论如何,我可以将Clojure符号的内容复制到一个新的符号中,以便第一个可以在不影响第二个的情况下进行变异吗?
这是我失败的尝试之一:
(var mat1 (clatrix/matrix (clatrix/ones 2 2)))
(var mat1)
(intern 'analyzer.core 'mat1 (clatrix/matrix (clatrix/ones 2 2)))
mat1
(intern 'analyzer.core 'mat2 mat1)
mat2
(clatrix/set mat1 0 0 2)
mat1
mat2
Run Code Online (Sandbox Code Playgroud)
当然,这不起作用:
(def mat1 (clatrix/matrix (clatrix/ones 2 2)
(def mat2 mat1)
Run Code Online (Sandbox Code Playgroud)
我也尝试过(但不确定我是否正好在这里做):
(def mat1 (clatrix/matrix (clatrix/ones 2 2)
(def mat2 `mat1)
Run Code Online (Sandbox Code Playgroud)
和
(def mat1 (clatrix/matrix (clatrix/ones 2 2))
(def mat2 ~mat1)
Run Code Online (Sandbox Code Playgroud)
和
(def mat1 (clatrix/matrix (clatrix/ones 2 2))
(def mat2 (.dup mat1))
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
更新
我对迄今为止提出的答案进行了基准测试.我不确定线条的符号是什么意思.建立:
(def mat1 (clatrix/ones 1000 1000) ; …Run Code Online (Sandbox Code Playgroud) 我想用置信区间计算我的数据中的一组组的总结统计数据(例如年龄,性别).为此,我使用泊松分布中的蒙特卡罗模拟绘图值来获取数据中的每一行,然后折叠行以获得摘要.整个手术过程工作正常,如果模拟的结果(使用rclass返回标)只是一个值,但只要我尝试模拟多个结果(使用ereturn中的eclass矩阵),它是不工作(见下面的Stata代码).我收到错误消息:"表达式中的类型不匹配错误:e(A)".如何在没有更复杂的循环等的情况下模拟整个矢量甚至结果矩阵?
非常感谢!弗雷德
program bootPGW, eclass
use "C:\Users\649007\Desktop\Demetriq_PWG_edu.dta", replace
gen id=_n
sort id
gen N=_N
by id: gen DL2=floor(rpoisson(calung))
by id:gen D02=floor(rpoisson(D0))
by id:gen Dsmoking=floor(rpoisson(smoking))
by id:gen ML2=(DL2/numpyr)*1000
by id:gen AL2=(ML2-CPSIIrate)/ML2
by id:replace AL2=0 if AL2<0
by id:gen A02=1-exp(-PWGcoef*(ML2-CPSIIrate))
by id:gen A2=(AL2*DL2+A02*D02)/(DL2+D02)
gen Adeaths=totdeath*A2
collapse (sum) Adeaths=Adeaths totdeath=totdeath Dsmoking=Dsmoking, by(edu_3cat sex country year)
gen AF_PWG=Adeaths/totdeath
gen AF_simple=Dsmoking/totdeath
mkmat AF_PWG, matrix(A)
ereturn matrix A=A
end
simulate a=e(A), reps(1000) nodots seed(123): bootPGW
Run Code Online (Sandbox Code Playgroud) I have a function f which takes 2 matrices with the same number of rows and procudes a scalar value. A am now looking for a possibility to create a new function which takes two lists of matrices and calls f for all pairs.
I need a moore efficient implementation of this loop:
% X = cell of matrices
% Y = cell of matrices
for k=1:length(X)
for l=1:length(Y)
M(k,l) = f(X{k},Y{l});
end
end
Run Code Online (Sandbox Code Playgroud)
(It is not a requirement that …
我有一个包含3列的数据框:
df<-data.frame(x=c(1,1,1,2,2,2,2,3,3), y=c(1,2,3,1,2,3,4,1,2), percentage=c(50,25,25,15,35,25,25,55,45))
Run Code Online (Sandbox Code Playgroud)
看起来像:
x y percentage
1 1 1 50
2 1 2 25
3 1 3 25
4 2 1 15
5 2 2 35
6 2 3 25
7 2 4 25
8 3 1 55
9 3 2 45
Run Code Online (Sandbox Code Playgroud)
第三列表示x-ID对象(1 col)内y-ID对象(2 col)的面积百分比。
我想得到一个带有x和y的矩阵(或与smthg相关),它们定义了坐标/下标和“百分比”,即矩阵的元素。
基本上,我想得到一个像这样的矩阵:
1 2 3 4
1 50 25 25 0
2 15 35 25 25
3 55 45 0 0
Run Code Online (Sandbox Code Playgroud)
有一个简单的方法可以做到这一点吗?
我有一个小矩阵:
SMALL<-matrix(c(1:9),3, 3)
colnames(SMALL)<-c("25","36","48")
rownames(SMALL)<-c("18","25","48")
Run Code Online (Sandbox Code Playgroud)
好像:
25 36 48
18 1 4 7
25 2 5 8
48 3 6 9
Run Code Online (Sandbox Code Playgroud)
还有一个大矩阵:
LARGE<-matrix(0,4, 4)
colnames(LARGE)<-c("12","25","36","48")
rownames(LARGE)<-c("18","25","38","48")
Run Code Online (Sandbox Code Playgroud)
好像:
12 25 36 48
18 0 0 0 0
25 0 0 0 0
38 0 0 0 0
48 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
我想用基于列/行名称的小矩阵替换大矩阵中的值.
寻找这个结果:
12 25 36 48
18 0 1 4 7
25 0 2 5 8
38 0 0 0 0
48 0 3 6 9
Run Code Online (Sandbox Code Playgroud)
有任何想法吗 ?