gpt4 book ai didi

matrix - Julia 相当于 Matlab 的 “logical” 矩阵

转载 作者:行者123 更新时间:2023-12-05 02:58:50 25 4
gpt4 key购买 nike

是否有 Julia 等价于 Matlab 的“逻辑”矩阵,您可以使用它来标记矩阵中的某些位置,然后将其用于矩阵操作?

在 matlab 中它看起来像这样:

A=magic(3);
C=eye(size(A));
C=logical(C);
M=A;
M(C)=0;

我需要在主对角线上保留零。在 matlab 中我会这样做,但在 Julia 中没有“逻辑”矩阵。我搜索了 Julia 等价物,但我找不到任何东西。提前致谢!

最佳答案

您可以创建一个 BitArrayBoolArray,用于 most intents and purposes the same .

例如

> using LinearAlgebra

>I(3) # `I()` is the identity matrix function
3×3 Diagonal{Bool,Array{Bool,1}}:
1 ⋅ ⋅
⋅ 1 ⋅
⋅ ⋅ 1

您可以使用它来将另一个矩阵中的元素置零 broadcasting逻辑非运算符 ~,然后将另一个矩阵中的每个元素相乘(通过广播 *.*)。

例如:

> x = reshape(1:9,3,3)
1 4 7
2 5 8
3 6 9

> x .* .~I(3)
0 4 7
2 0 8
3 6 0

关于matrix - Julia 相当于 Matlab 的 “logical” 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58584146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com