Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从matlab到python的随机三对角矩阵_Python_Matlab_Numpy - Fatal编程技术网

从matlab到python的随机三对角矩阵

从matlab到python的随机三对角矩阵,python,matlab,numpy,Python,Matlab,Numpy,我想尝试将以下代码从Matlab实现到Python我一般不熟悉Python,但我尝试使用basics从Matlab翻译它 % n is random integer from 1 to 10 % first set the random seed (because we want our results to be reproducible; % the seed sets a starting point in the sequence of random numbers the progra

我想尝试将以下代码从Matlab实现到Python我一般不熟悉Python,但我尝试使用basics从Matlab翻译它

% n is random integer from 1 to 10
% first set the random seed (because we want our results to be reproducible;
% the seed sets a starting point in the sequence of random numbers the program
rng(n)
 
% Generate random columns
a = rand(n, 1);
b = rand(n, 1);
c = rand(n, 1);

% Convert to a matrix
A = zeros(n);
for i = 1:n
    if i ~= n
        A(i + 1, i) = a(i + 1);
        A(i, i + 1) = c(i);
    end
    A(i, i) = b(i);
end
这是我在Python中的尝试:

import numpy as np
## n is random  integer  from 1 to 10
np.random.seed(n)

### generate random columns: 
a = np.random.rand(n)
b = np.random.rand(n)
c = np.random.rand(n)

A = np.zeros((n, n)) ## create zero n-by-n  matrix
for i in range(0, n):
    if  (i != n):
         A[i + 1, i] = a[i + 1]
         A[i, i + 1] = c[i]
    A[i, i] = b[i]
我在A[I+1,I]=A[I]行遇到错误。Python中是否有我在这里遗漏的结构?

简短的回答是,对于I=1:n迭代[1,n],包括两个边界,而对于range中的I:iterates[0,n,排他在右边界。因此,检查I~=n是否正确测试您是否在右边界,而如果I!=n:不在右边界。将其替换为

if  i != n - 1:
很长的答案是,在这两种语言中,您都不需要任何代码,因为MATLAB和numpy都用于矢量化操作

A = diag(a(2:end), -1) + diag(b, 0) + diag(c(1:end-1), +1)
在努比,情况非常相似:

A = np.diag(a[1:], -1) + np.diag(b, 0) + np.diag(c[:-1], +1)
您还可以使用其他技巧,特别是如果您只想在矩阵中使用随机数:

A = np.random.rand(n, n)
A[np.tril_indices(n, -2)] = A[np.triu_indices(n, 2)] = 0
您可以使用其他基于索引的方法:

i, j = np.diag_indices(n)
i = np.concatenate((i[:-1], i, i[1:]))
j = np.concatenate((j[1:], j, j[:-1]))
A = np.zeros((n, n))
A[i, j] = np.random.rand(3 * n - 2)
简而言之,对于i=1:n迭代[1,n],包含在两个边界上,而对于range中的i:iterates[0,n,独占在右边界上。因此,检查i~=n是否正确测试您是否位于右边界,而如果i!=n:不在右边界上。将其替换为

if  i != n - 1:
很长的答案是,在这两种语言中,您都不需要任何代码,因为MATLAB和numpy都用于矢量化操作

A = diag(a(2:end), -1) + diag(b, 0) + diag(c(1:end-1), +1)
在努比,情况非常相似:

A = np.diag(a[1:], -1) + np.diag(b, 0) + np.diag(c[:-1], +1)
您还可以使用其他技巧,特别是如果您只想在矩阵中使用随机数:

A = np.random.rand(n, n)
A[np.tril_indices(n, -2)] = A[np.triu_indices(n, 2)] = 0
您可以使用其他基于索引的方法:

i, j = np.diag_indices(n)
i = np.concatenate((i[:-1], i, i[1:]))
j = np.concatenate((j[1:], j, j[:-1]))
A = np.zeros((n, n))
A[i, j] = np.random.rand(3 * n - 2)

正如上面的评论清楚地指出了索引错误,以下是一种基于以下内容的简单方法:


正如上面的评论清楚地指出了索引错误,以下是一种基于以下内容的简单方法:


当i=n-1时,i+1对于nxn矩阵来说太大,在python中索引为0..n-1若要生成MCVE,请将n设置为特定值。正确的检查是i!=n-1,因为基于零的索引当i=n-1时,i+1对于nxn矩阵来说太大,而在python中索引为0..n-1若要生成MCVE,请将n设置为特定值。正确的检查是i!=n-1 b因为零基索引比我快:@MadPhysician够近了:比我快了:@MadPhysician够近了: