【问题标题】:Julia: How to make a matrix in for loopJulia:如何在 for 循环中制作矩阵
【发布时间】:2023-01-02 16:34:56
【问题描述】:

我是朱莉娅的新手。在那之前,我使用 Matlab。 对于 Matlab 的情况,我写了命令在 for 循环中制作矩阵,如下所示:

for i=1:1:100; k(i,:)=i.^2; end

我在 Julia 中输入了相同的命令,但没有用。 此外,我尝试了如下其他命令:

n=100;
k = Array(Int64, n);
for i in 1:n;
    k[i]= i;
end;

但是,错误发生如下:

MethodError: no method matching setindex!(::Type{Array{Int64, 10}}, ::Int64, ::Int64)

如何在 Julia 的 for 循环中创建矩阵?

【问题讨论】:

    标签: arrays matrix julia


    【解决方案1】:

    您可以遵循以下方法:

    julia> n = 100;
    
    julia> k = Array{Int64, 1}(undef, n);
    
    julia> for i in 1:n
               k[i]=i
           end
    
    julia> k
    100-element Vector{Int64}:
       1
       2
       3
       4
       5
       6
       ⋮
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多